From bf90e2138c9bf2730fe840781b735653bcbdf12c Mon Sep 17 00:00:00 2001 From: SoXX Date: Wed, 6 Nov 2024 11:48:01 +0100 Subject: [PATCH] feat(database): added CRUD --- pkg/database/post_report.go | 173 ++++++++++++++++++++++++++++++++++++ 1 file changed, 173 insertions(+) create mode 100644 pkg/database/post_report.go diff --git a/pkg/database/post_report.go b/pkg/database/post_report.go new file mode 100644 index 0000000..e531952 --- /dev/null +++ b/pkg/database/post_report.go @@ -0,0 +1,173 @@ +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 CreatePostReport(ctx context.Context, postReport models.PostReport) (models.PostReport, error) { + ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "CreatePostReport") + defer span.End() + + localLogger = localLogger.WithFields(log.Fields{ + "post_report_id": postReport.ID, + }) + + span.SetAttributes( + attribute.String("post_report_id", string(postReport.ID)), + ) + + utils.HandleEvent(span, localLogger, "Starting postReport creation") + + if client == nil { + return models.PostReport{}, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected}) + } + + result := client.WithContext(ctx).Create(&postReport) + if result.Error != nil { + if errors.Is(result.Error, gorm.ErrDuplicatedKey) { + return models.PostReport{}, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DuplicateKey}) + } + return models.PostReport{}, utils.HandleError(ctx, span, localLogger, result.Error) + } + + utils.HandleEvent(span, localLogger, "Post createdReport successfully") + return postReport, nil +} + +func GetPostReportByID(ctx context.Context, id models.PostReportID) (models.PostReport, error) { + ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "GetPostReportByID") + defer span.End() + + localLogger = localLogger.WithFields(log.Fields{ + "post_report_id": id, + }) + + span.SetAttributes( + attribute.String("post_report_id", string(id)), + ) + + utils.HandleEvent(span, localLogger, "Starting get postReport by ID") + + var postReport models.PostReport + + if client == nil { + return models.PostReport{}, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected}) + } + + if len(id) == 0 { + return models.PostReport{}, utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.PostReportIDIsEmpty}) + } + + if len(id) != 25 { + return models.PostReport{}, utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.PostReportIDIsWrongLength}) + } + + result := client.WithContext(ctx).First(&postReport, "id = ?", id) + if result.Error != nil { + if errors.Is(result.Error, gorm.ErrRecordNotFound) { + return models.PostReport{}, utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.NoDataFound}) + } + return models.PostReport{}, utils.HandleError(ctx, span, localLogger, result.Error) + } + + utils.HandleEvent(span, localLogger, "PostReport retrieved successfully") + return postReport, nil +} + +// UpdatePostReport updates the PostReport information in the database. +// Only a few parameter can be updated: +// - AuditBy +// - AuditDescription +// - ReportType +// - ReportState +func UpdatePostReport(ctx context.Context, postReport models.PostReport) error { + ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "UpdatePostReport") + defer span.End() + + localLogger = localLogger.WithFields(log.Fields{ + "post_report_id": postReport.ID, + }) + + span.SetAttributes( + attribute.String("post_report_id", string(postReport.ID)), + ) + + utils.HandleEvent(span, localLogger, "Starting postReport update") + + if client == nil { + return utils.HandleError(ctx, span, localLogger, &otterError.Database{Reason: otterError.DatabaseIsNotConnected}) + } + + if len(postReport.ID) == 0 { + return utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.PostIDIsEmpty}) + } + + updatePost := models.PostReport{ + BaseModel: models.BaseModel[models.PostReportID]{ + ID: postReport.ID, + }, + AuditBy: postReport.AuditBy, + AuditDescription: postReport.AuditDescription, + ReportType: postReport.ReportType, + ReportState: postReport.ReportState, + } + + result := client.WithContext(ctx).Model(&updatePost).Update("deleted_at", gorm.DeletedAt{}) + 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, "PostReport updated successfully") + return nil +} + +func DeletePostReport(ctx context.Context, id models.PostReportID) error { + ctx, span, localLogger := utils.SetupTracing(ctx, tracer, "DeletePostReport") + defer span.End() + + localLogger = localLogger.WithFields(log.Fields{ + "post_report_id": id, + }) + + span.SetAttributes( + attribute.String("post_report_id", string(id)), + ) + + utils.HandleEvent(span, localLogger, "Starting delete postReport") + + var postReport models.PostReport + + 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.PostReportIDIsEmpty}) + } + + if len(id) != 25 { + return utils.HandleError(ctx, span, localLogger, &otterError.EntityValidationFailed{Reason: otterError.PostReportIDIsWrongLength}) + } + + result := client.WithContext(ctx).Delete(&postReport, "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, "PostReport deleted successfully") + return nil +}