feat(database): added CRUD

- added Create
- added Get
- added Update
- added Delete
This commit is contained in:
SoXX 2024-10-15 12:25:32 +02:00
parent 4312cbdff5
commit 27bc719a02
4 changed files with 178 additions and 7 deletions

View File

@ -0,0 +1,166 @@
package database
import (
"context"
"errors"
"git.anthrove.art/Anthrove/otter-space-sdk/v4/internal/utils"
otterError "git.anthrove.art/Anthrove/otter-space-sdk/v4/pkg/error"
"git.anthrove.art/Anthrove/otter-space-sdk/v4/pkg/models"
log "github.com/sirupsen/logrus"
"go.opentelemetry.io/otel/attribute"
"gorm.io/gorm"
)
func CreateScrapeHistory(ctx context.Context, scrapeHistory models.ScrapeHistory) (models.ScrapeHistory, error) {
ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "CreateScrapeHistory")
defer span.End()
localLogger = localLogger.WithFields(log.Fields{
"scrape_task_id": scrapeHistory.ScrapeTaskID,
"user_source_id": scrapeHistory.UserSourceID,
})
span.SetAttributes(
attribute.String("scrape_task_id", string(scrapeHistory.ScrapeTaskID)),
attribute.String("user_source_id", string(scrapeHistory.UserSourceID)),
)
utils.HandleEvent(span, localLogger, "Starting scrapeHistory creation")
if client == nil {
return models.ScrapeHistory{}, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected})
}
result := client.WithContext(ctx).Create(&scrapeHistory)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrDuplicatedKey) {
return models.ScrapeHistory{}, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DuplicateKey})
}
return models.ScrapeHistory{}, utils.HandleError(ctx, span, localLogger, result.Error)
}
utils.HandleEvent(span, localLogger, "scrapeHistory created successfully")
return scrapeHistory, nil
}
func GetScrapeHistoryByID(ctx context.Context, id models.ScrapeTaskID) (models.ScrapeHistory, error) {
ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "GetScrapeHistoryByID")
defer span.End()
localLogger = localLogger.WithFields(log.Fields{
"scrape_task_id": id,
})
span.SetAttributes(
attribute.String("scrape_task_id", string(id)),
)
utils.HandleEvent(span, localLogger, "Starting get scrapeHistory by ID")
var post models.ScrapeHistory
if client == nil {
return models.ScrapeHistory{}, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected})
}
if len(id) == 0 {
return models.ScrapeHistory{}, utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.ScrapeTaskIDIsEmpty})
}
result := client.WithContext(ctx).First(&post, "scrape_task_id = ?", id)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return models.ScrapeHistory{}, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.NoDataFound})
}
return models.ScrapeHistory{}, utils.HandleError(ctx, span, localLogger, result.Error)
}
utils.HandleEvent(span, localLogger, "scrapeHistory retrieved successfully")
return post, nil
}
// UpdateScrapeHistory updates the scrape history information in the database.
// Only a few parameter can be updated:
// - FinishedAt
// - Error
// - AddedPosts
// - DeletedPosts
func UpdateScrapeHistory(ctx context.Context, scrapeHistory models.ScrapeHistory) error {
ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "UpdateScrapeHistory")
defer span.End()
localLogger = localLogger.WithFields(log.Fields{
"scrape_task_id": scrapeHistory.ScrapeTaskID,
})
span.SetAttributes(
attribute.String("scrape_task_id", string(scrapeHistory.ScrapeTaskID)),
)
utils.HandleEvent(span, localLogger, "Starting scrapeHistory update")
if client == nil {
return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected})
}
if len(scrapeHistory.ScrapeTaskID) == 0 {
return utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.ScrapeTaskIDIsEmpty})
}
updateScrapeHistory := models.ScrapeHistory{
ScrapeTaskID: scrapeHistory.ScrapeTaskID,
UserSourceID: scrapeHistory.UserSourceID,
FinishedAt: scrapeHistory.FinishedAt,
Error: scrapeHistory.Error,
AddedPosts: scrapeHistory.AddedPosts,
DeletedPosts: scrapeHistory.DeletedPosts,
}
result := client.WithContext(ctx).Model(&updateScrapeHistory).Updates(updateScrapeHistory)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.NoDataFound})
}
return utils.HandleError(ctx, span, localLogger, result.Error)
}
utils.HandleEvent(span, localLogger, "scrapeHistory updated successfully")
return nil
}
func DeleteScrapeHistory(ctx context.Context, id models.ScrapeTaskID) error {
ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "DeleteScrapeHistory")
defer span.End()
localLogger = localLogger.WithFields(log.Fields{
"scrape_task_id": id,
})
span.SetAttributes(
attribute.String("scrape_task_id", string(id)),
)
utils.HandleEvent(span, localLogger, "Starting delete scrapeHistory")
var scrapeHistory models.ScrapeHistory
if client == nil {
return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected})
}
if len(id) == 0 {
return utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.ScrapeTaskIDIsEmpty})
}
result := client.WithContext(ctx).Delete(&scrapeHistory, "scrape_task_id = ?", id)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.NoDataFound})
}
return utils.HandleError(ctx, span, localLogger, result.Error)
}
utils.HandleEvent(span, localLogger, "scrapeHistory deleted successfully")
return nil
}

View File

@ -35,6 +35,9 @@ const (
PoolIDIsWrongLength = "PoolID has the wrong length"
PoolURLIsEmpty = "PoolURL cannot be empty"
ScrapeTaskIDIsEmpty = "ScrapeTaskID cannot be empty"
ScrapeTaskIDIsWrongLength = "ScrapeTaskID has the wrong length"
)
type EntityValidationFailed struct {

View File

@ -23,6 +23,8 @@ type (
UserSourceID string
UserFavoriteID string
ScrapeTaskID string
)
const (

View File

@ -5,10 +5,10 @@ import (
)
type ScrapeHistory struct {
ScrapeTaskID string `json:"scrape_task_id" gorm:"primaryKey"`
UserSourceID string `json:"user_source_id" gorm:""`
ScrapeTaskID ScrapeTaskID `json:"scrape_task_id" gorm:"primaryKey"`
UserSourceID UserSourceID `json:"user_source_id" gorm:""`
CreatedAt time.Time `json:"created_at" gorm:""`
FinishedAt time.Time `json:"finished_at" gorm:"null"`
FinishedAt time.Time `json:"finished_at" gorm:""`
Error string `json:"error" gorm:"null"`
AddedPosts int `json:"added_posts" gorm:"not null"`
DeletedPosts int `json:"deleted_posts" gorm:"not null"`