Compare commits

...

11 Commits
v4.1.0 ... main

Author SHA1 Message Date
da76e6215d fix(module): updated to version
All checks were successful
Gitea Build Check / Build (push) Successful in 3m12s
2024-11-06 13:21:21 +01:00
18a68b452f Merge branch 'dev/post-reports' of git.anthrove.art:Anthrove/otter-space-sdk into dev/post-reports
All checks were successful
Gitea Build Check / Build (pull_request) Successful in 2m57s
Gitea Build Check / Build (push) Successful in 2m58s
2024-11-06 12:22:27 +01:00
1ae76234b2 fix(database): update function 2024-11-06 12:20:24 +01:00
8bb2328307 Merge branch 'main' into dev/post-reports
All checks were successful
Gitea Build Check / Build (pull_request) Successful in 3m2s
2024-11-06 11:11:59 +00:00
d69d1b68f6 feat(database): added tests
All checks were successful
Gitea Build Check / Build (pull_request) Successful in 2m58s
2024-11-06 12:11:15 +01:00
bf90e2138c feat(database): added CRUD 2024-11-06 11:48:01 +01:00
797aaa3723 feat(models): added new PostReport model 2024-11-06 11:47:48 +01:00
55f5795719 feat(migration): added new PostReport model 2024-11-06 11:22:22 +01:00
e53fd2c9a5 feat(migration): added migration toggle
All checks were successful
Gitea Build Check / Build (pull_request) Successful in 3m1s
Gitea Build Check / Build (push) Successful in 2m49s
2024-11-06 10:55:47 +01:00
d5f0d3ee8c fix(database): migration
All checks were successful
Gitea Build Check / Build (pull_request) Successful in 3m40s
Gitea Build Check / Build (push) Successful in 3m36s
- fix wrong id types
2024-10-15 15:20:30 +02:00
104c148eb8 fix(database): pool model 2024-10-15 15:20:11 +02:00
43 changed files with 869 additions and 93 deletions

View File

@ -1,4 +1,4 @@
![Build Check Runner](https://git.anthrove.art/Anthrove/otter-space-sdk/v4/actions/workflows/build_check.yaml/badge.svg)
![Build Check Runner](https://git.anthrove.art/Anthrove/otter-space-sdk/v5/actions/workflows/build_check.yaml/badge.svg)
[![Bugs](https://sonarqube.dragse.de/api/project_badges/measure?project=Anthrove---OtterSpace-SDK&metric=bugs&token=sqb_96012ffdd64ce721d7f9c82bfa77aa27a5c1fd38)](https://sonarqube.dragse.de/dashboard?id=Anthrove---OtterSpace-SDK)
[![Code Smells](https://sonarqube.dragse.de/api/project_badges/measure?project=Anthrove---OtterSpace-SDK&metric=code_smells&token=sqb_96012ffdd64ce721d7f9c82bfa77aa27a5c1fd38)](https://sonarqube.dragse.de/dashboard?id=Anthrove---OtterSpace-SDK)
[![Coverage](https://sonarqube.dragse.de/api/project_badges/measure?project=Anthrove---OtterSpace-SDK&metric=coverage&token=sqb_96012ffdd64ce721d7f9c82bfa77aa27a5c1fd38)](https://sonarqube.dragse.de/dashboard?id=Anthrove---OtterSpace-SDK)
@ -22,7 +22,7 @@ The OtterSpace SDK is a Go package for interacting with the OtterSpace API. It p
To install the OtterSpace SDK, you can use `go get`:
```shell
go get git.anthrove.art/Anthrove/otter-space-sdk/v4
go get git.anthrove.art/Anthrove/otter-space-sdk/v5
````
## Usage
@ -34,8 +34,8 @@ package main
import (
"context"
"git.anthrove.art/Anthrove/otter-space-sdk/v4/pkg/database"
"git.anthrove.art/Anthrove/otter-space-sdk/v4/pkg/models"
"git.anthrove.art/Anthrove/otter-space-sdk/v5/pkg/database"
"git.anthrove.art/Anthrove/otter-space-sdk/v5/pkg/models"
)
func main() {

2
go.mod
View File

@ -1,4 +1,4 @@
module git.anthrove.art/Anthrove/otter-space-sdk/v4
module git.anthrove.art/Anthrove/otter-space-sdk/v5
go 1.22.0

View File

@ -5,9 +5,9 @@ import (
"embed"
"fmt"
"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"
"git.anthrove.art/Anthrove/otter-space-sdk/v5/internal/utils"
otterError "git.anthrove.art/Anthrove/otter-space-sdk/v5/pkg/error"
"git.anthrove.art/Anthrove/otter-space-sdk/v5/pkg/models"
migrate "github.com/rubenv/sql-migrate"
log "github.com/sirupsen/logrus"
"go.opentelemetry.io/contrib/bridges/otellogrus"
@ -17,7 +17,7 @@ import (
"gorm.io/gorm"
)
const tracingName = "git.anthrove.art/Anthrove/otter-space-sdk/v4/pkg/database"
const tracingName = "git.anthrove.art/Anthrove/otter-space-sdk/v5/pkg/database"
var (
//go:embed migrations/*.sql
@ -70,9 +70,11 @@ func Connect(ctx context.Context, config models.DatabaseConfig) error {
return utils.HandleError(ctx, span, localLogger, err)
}
err = migrateDatabase(ctx, sqlDB, config)
if err != nil {
return utils.HandleError(ctx, span, localLogger, err)
if config.Migrate {
err = migrateDatabase(ctx, sqlDB, config)
if err != nil {
return utils.HandleError(ctx, span, localLogger, err)
}
}
client = sqlDB

View File

@ -4,8 +4,8 @@ import (
"context"
"testing"
"git.anthrove.art/Anthrove/otter-space-sdk/v4/pkg/models"
"git.anthrove.art/Anthrove/otter-space-sdk/v4/test"
"git.anthrove.art/Anthrove/otter-space-sdk/v5/pkg/models"
"git.anthrove.art/Anthrove/otter-space-sdk/v5/test"
"go.opentelemetry.io/contrib/bridges/otellogrus"
"go.opentelemetry.io/otel"
"gorm.io/gorm"

View File

@ -4,9 +4,9 @@ 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"
"git.anthrove.art/Anthrove/otter-space-sdk/v5/internal/utils"
otterError "git.anthrove.art/Anthrove/otter-space-sdk/v5/pkg/error"
"git.anthrove.art/Anthrove/otter-space-sdk/v5/pkg/models"
log "github.com/sirupsen/logrus"
"go.opentelemetry.io/otel/attribute"
"gorm.io/gorm"

View File

@ -7,8 +7,8 @@ import (
"testing"
"time"
"git.anthrove.art/Anthrove/otter-space-sdk/v4/pkg/models"
"git.anthrove.art/Anthrove/otter-space-sdk/v4/test"
"git.anthrove.art/Anthrove/otter-space-sdk/v5/pkg/models"
"git.anthrove.art/Anthrove/otter-space-sdk/v5/test"
"go.opentelemetry.io/contrib/bridges/otellogrus"
"go.opentelemetry.io/otel"
"gorm.io/gorm"

View File

@ -0,0 +1,13 @@
-- +migrate Up
ALTER TABLE "PostReference"
ALTER COLUMN post_id TYPE CHAR(25),
ALTER COLUMN source_id TYPE CHAR(25);
ALTER TABLE post_tags
ALTER COLUMN post_id TYPE CHAR(25);
ALTER TABLE "UserFavorites"
ALTER COLUMN post_id TYPE CHAR(25);
ALTER TABLE "UserSource"
ALTER COLUMN source_id TYPE CHAR(25);

View File

@ -0,0 +1,30 @@
-- +migrate Up
CREATE TYPE ReportType AS ENUM (
'duplicate',
'missing_data',
'rating_abuse',
'illegal_content'
);
CREATE TYPE ReportState AS ENUM (
'pending_unclaimed',
'pending',
'approved',
'partial',
'rejected'
);
CREATE TABLE "PostReport"
(
id CHAR(25) NOT NULL PRIMARY KEY,
created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
deleted_at TIMESTAMP WITH TIME ZONE NULL,
post_id CHAR(25) NOT NULL REFERENCES "Post" (id),
report_by TEXT NOT NULL REFERENCES "User" (id),
report_description TEXT NOT NULL,
audit_by TEXT NULL REFERENCES "User" (id),
audit_description TEXT NOT NULL,
report_type ReportType NOT NULL,
report_state ReportState NOT NULL
);

View File

@ -4,9 +4,9 @@ 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"
"git.anthrove.art/Anthrove/otter-space-sdk/v5/internal/utils"
otterError "git.anthrove.art/Anthrove/otter-space-sdk/v5/pkg/error"
"git.anthrove.art/Anthrove/otter-space-sdk/v5/pkg/models"
log "github.com/sirupsen/logrus"
"go.opentelemetry.io/otel/attribute"
"gorm.io/gorm"

View File

@ -4,9 +4,9 @@ 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"
"git.anthrove.art/Anthrove/otter-space-sdk/v5/internal/utils"
otterError "git.anthrove.art/Anthrove/otter-space-sdk/v5/pkg/error"
"git.anthrove.art/Anthrove/otter-space-sdk/v5/pkg/models"
log "github.com/sirupsen/logrus"
"go.opentelemetry.io/otel/attribute"
"gorm.io/gorm"

View File

@ -6,8 +6,8 @@ import (
"reflect"
"testing"
"git.anthrove.art/Anthrove/otter-space-sdk/v4/pkg/models"
"git.anthrove.art/Anthrove/otter-space-sdk/v4/test"
"git.anthrove.art/Anthrove/otter-space-sdk/v5/pkg/models"
"git.anthrove.art/Anthrove/otter-space-sdk/v5/test"
"go.opentelemetry.io/contrib/bridges/otellogrus"
"go.opentelemetry.io/otel"
)

View File

@ -4,9 +4,9 @@ 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"
"git.anthrove.art/Anthrove/otter-space-sdk/v5/internal/utils"
otterError "git.anthrove.art/Anthrove/otter-space-sdk/v5/pkg/error"
"git.anthrove.art/Anthrove/otter-space-sdk/v5/pkg/models"
log "github.com/sirupsen/logrus"
"go.opentelemetry.io/otel/attribute"
"gorm.io/gorm"

View File

@ -6,8 +6,8 @@ import (
"reflect"
"testing"
"git.anthrove.art/Anthrove/otter-space-sdk/v4/pkg/models"
"git.anthrove.art/Anthrove/otter-space-sdk/v4/test"
"git.anthrove.art/Anthrove/otter-space-sdk/v5/pkg/models"
"git.anthrove.art/Anthrove/otter-space-sdk/v5/test"
"go.opentelemetry.io/contrib/bridges/otellogrus"
"go.opentelemetry.io/otel"
)

View File

@ -7,8 +7,8 @@ import (
"testing"
"time"
"git.anthrove.art/Anthrove/otter-space-sdk/v4/pkg/models"
"git.anthrove.art/Anthrove/otter-space-sdk/v4/test"
"git.anthrove.art/Anthrove/otter-space-sdk/v5/pkg/models"
"git.anthrove.art/Anthrove/otter-space-sdk/v5/test"
"go.opentelemetry.io/contrib/bridges/otellogrus"
"go.opentelemetry.io/otel"
"gorm.io/gorm"

View File

@ -4,9 +4,9 @@ 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"
"git.anthrove.art/Anthrove/otter-space-sdk/v5/internal/utils"
otterError "git.anthrove.art/Anthrove/otter-space-sdk/v5/pkg/error"
"git.anthrove.art/Anthrove/otter-space-sdk/v5/pkg/models"
log "github.com/sirupsen/logrus"
"go.opentelemetry.io/otel/attribute"
"gorm.io/gorm"

173
pkg/database/post_report.go Normal file
View File

@ -0,0 +1,173 @@
package database
import (
"context"
"errors"
"git.anthrove.art/Anthrove/otter-space-sdk/v5/internal/utils"
otterError "git.anthrove.art/Anthrove/otter-space-sdk/v5/pkg/error"
"git.anthrove.art/Anthrove/otter-space-sdk/v5/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).Updates(updatePost)
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
}

View File

@ -0,0 +1,507 @@
package database
import (
"context"
"fmt"
"reflect"
"testing"
"time"
"git.anthrove.art/Anthrove/otter-space-sdk/v5/pkg/models"
"git.anthrove.art/Anthrove/otter-space-sdk/v5/test"
"go.opentelemetry.io/contrib/bridges/otellogrus"
"go.opentelemetry.io/otel"
"gorm.io/gorm"
)
func TestCreatePostReport(t *testing.T) {
// Setup trow away container
ctx := context.Background()
container, gormDB, err := test.StartPostgresContainer(ctx)
if err != nil {
logger.Fatalf("Could not start PostgreSQL container: %v", err)
}
client = gormDB
// Setup open telemetry
tracer = otel.Tracer(tracingName)
hook := otellogrus.NewHook(tracingName)
logger.AddHook(hook)
defer container.Terminate(ctx)
// -- -- Setup Tests
// -- Create Post to test with
validPost := models.Post{
BaseModel: models.BaseModel[models.PostID]{
ID: models.PostID(fmt.Sprintf("%025s", "Post1")),
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
DeletedAt: gorm.DeletedAt{},
},
Rating: models.SFW,
}
validPost, err = CreatePost(ctx, validPost)
if err != nil {
t.Fatalf("CreatePostReport err: %v", err)
}
// Setup Tests
validUser01 := models.User{BaseModel: models.BaseModel[models.UserID]{ID: models.UserID(fmt.Sprintf("%025s", "User1"))}}
validUser01, err = CreateUser(ctx, validUser01)
if err != nil {
t.Fatalf("CreatePostReport err: %v", err)
}
validUser02 := models.User{BaseModel: models.BaseModel[models.UserID]{ID: models.UserID(fmt.Sprintf("%025s", "User2"))}}
validUser02, err = CreateUser(ctx, validUser02)
if err != nil {
t.Fatalf("CreatePostReport err: %v", err)
}
// -- Create PostReport to test with
validPostReport := models.PostReport{
BaseModel: models.BaseModel[models.PostReportID]{
ID: models.PostReportID(fmt.Sprintf("%025s", "PostReport1")),
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
DeletedAt: gorm.DeletedAt{},
},
PostID: validPost.ID,
ReportBy: validUser01.ID,
ReportDescription: "aaa",
AuditBy: &validUser02.ID,
AuditDescription: "ahaa",
ReportType: models.Duplicate,
ReportState: models.Rejected,
}
// --
// -- -- Tests
type args struct {
ctx context.Context
source models.PostReport
}
tests := []struct {
name string
args args
want models.PostReport
wantErr bool
}{
{
name: "Test 01: Valid PostReport",
args: args{
ctx: ctx,
source: validPostReport,
},
want: validPostReport,
wantErr: false,
},
{
name: "Test 02: Duplicate PostReport",
args: args{
ctx: ctx,
source: validPostReport,
},
want: models.PostReport{},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := CreatePostReport(tt.args.ctx, tt.args.source)
if (err != nil) != tt.wantErr {
t.Errorf("CreatePostReport() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("CreatePostReport() got = %v, want %v", got, tt.want)
}
})
}
}
func TestUpdatePostReport(t *testing.T) {
// Setup trow away container
ctx := context.Background()
container, gormDB, err := test.StartPostgresContainer(ctx)
if err != nil {
logger.Fatalf("Could not start PostgreSQL container: %v", err)
}
client = gormDB
// Setup open telemetry
tracer = otel.Tracer(tracingName)
hook := otellogrus.NewHook(tracingName)
logger.AddHook(hook)
defer container.Terminate(ctx)
// -- Create Post to test with
validPost := models.Post{
BaseModel: models.BaseModel[models.PostID]{
ID: models.PostID(fmt.Sprintf("%025s", "Post1")),
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
DeletedAt: gorm.DeletedAt{},
},
Rating: models.SFW,
}
validPost, err = CreatePost(ctx, validPost)
if err != nil {
t.Fatalf("CreatePostReport err: %v", err)
}
// Setup Tests
validUser01 := models.User{BaseModel: models.BaseModel[models.UserID]{ID: models.UserID(fmt.Sprintf("%025s", "User1"))}}
validUser01, err = CreateUser(ctx, validUser01)
if err != nil {
t.Fatalf("CreatePostReport err: %v", err)
}
validUser02 := models.User{BaseModel: models.BaseModel[models.UserID]{ID: models.UserID(fmt.Sprintf("%025s", "User2"))}}
validUser02, err = CreateUser(ctx, validUser02)
if err != nil {
t.Fatalf("CreatePostReport err: %v", err)
}
// -- Create PostReport to test with
validPostReport := models.PostReport{
BaseModel: models.BaseModel[models.PostReportID]{
ID: models.PostReportID(fmt.Sprintf("%025s", "PostReport1")),
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
DeletedAt: gorm.DeletedAt{},
},
PostID: validPost.ID,
ReportBy: validUser01.ID,
ReportDescription: "aaa",
AuditBy: nil,
AuditDescription: "",
ReportType: models.Duplicate,
ReportState: models.Rejected,
}
// --
validPostReport, err = CreatePostReport(ctx, validPostReport)
if err != nil {
t.Fatalf("CreatePostReport err: %v", err)
}
// --
// -- Create Updates models for UserPostReport
validUpdatePostReport := validPostReport
validUpdatePostReport.AuditBy = &validUser02.ID
validUpdatePostReport.AuditDescription = "aaaaa"
validUpdatePostReport.ReportState = models.Approved
invalidUpdatePostReport := models.PostReport{}
// --
// -- -- Tests
type args struct {
ctx context.Context
source models.PostReport
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "Test 01: Valid Update for PostReport",
args: args{
ctx: ctx,
source: validUpdatePostReport,
},
wantErr: false,
},
{
name: "Test 02: Invalid Update for PostReport",
args: args{
ctx: ctx,
source: invalidUpdatePostReport,
},
wantErr: true,
},
{
name: "Test 03: Empty ID for Update for PostReport",
args: args{
ctx: ctx,
source: models.PostReport{BaseModel: models.BaseModel[models.PostReportID]{ID: ""}},
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := UpdatePostReport(tt.args.ctx, tt.args.source); (err != nil) != tt.wantErr {
t.Errorf("UpdatePostReport() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
func TestGetPostReportByID(t *testing.T) {
// Setup trow away container
ctx := context.Background()
container, gormDB, err := test.StartPostgresContainer(ctx)
if err != nil {
logger.Fatalf("Could not start PostgreSQL container: %v", err)
}
client = gormDB
// Setup open telemetry
tracer = otel.Tracer(tracingName)
hook := otellogrus.NewHook(tracingName)
logger.AddHook(hook)
defer container.Terminate(ctx)
// -- -- Setup Tests
// -- Create Post to test with
validPost := models.Post{
BaseModel: models.BaseModel[models.PostID]{
ID: models.PostID(fmt.Sprintf("%025s", "Post1")),
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
DeletedAt: gorm.DeletedAt{},
},
Rating: models.SFW,
}
validPost, err = CreatePost(ctx, validPost)
if err != nil {
t.Fatalf("CreatePostReport err: %v", err)
}
// Setup Tests
validUser01 := models.User{BaseModel: models.BaseModel[models.UserID]{ID: models.UserID(fmt.Sprintf("%025s", "User1"))}}
validUser01, err = CreateUser(ctx, validUser01)
if err != nil {
t.Fatalf("CreatePostReport err: %v", err)
}
validUser02 := models.User{BaseModel: models.BaseModel[models.UserID]{ID: models.UserID(fmt.Sprintf("%025s", "User2"))}}
validUser02, err = CreateUser(ctx, validUser02)
if err != nil {
t.Fatalf("CreatePostReport err: %v", err)
}
// -- Create PostReport to test with
validPostReport := models.PostReport{
BaseModel: models.BaseModel[models.PostReportID]{
ID: models.PostReportID(fmt.Sprintf("%025s", "PostReport1")),
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
DeletedAt: gorm.DeletedAt{},
},
PostID: validPost.ID,
ReportBy: validUser01.ID,
ReportDescription: "aaa",
AuditBy: nil,
AuditDescription: "",
ReportType: models.Duplicate,
ReportState: models.Rejected,
}
// --
validPostReport, err = CreatePostReport(ctx, validPostReport)
if err != nil {
t.Fatalf("CreatePostReport err: %v", err)
}
// --
// --
// -- -- Tests
type args struct {
ctx context.Context
id models.PostReportID
}
tests := []struct {
name string
args args
want models.PostReport
wantErr bool
}{
{
name: "Test 01: Valid PostReport ID",
args: args{
ctx: ctx,
id: validPostReport.ID,
},
want: validPostReport,
wantErr: false,
},
{
name: "Test 03: Empty PostReportID",
args: args{
ctx: ctx,
id: "",
},
wantErr: true,
},
{
name: "Test 04: Short PostReportID",
args: args{
ctx: ctx,
id: "111",
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := GetPostReportByID(tt.args.ctx, tt.args.id)
if (err != nil) != tt.wantErr {
t.Errorf("GetPostReportByID() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !checkPostReportID(got, tt.want) {
t.Errorf("GetPostReportByID() got = %v, want %v", got, tt.want)
}
})
}
}
func TestDeletePostReport(t *testing.T) {
// Setup trow away container
ctx := context.Background()
container, gormDB, err := test.StartPostgresContainer(ctx)
if err != nil {
logger.Fatalf("Could not start PostgreSQL container: %v", err)
}
client = gormDB
// Setup open telemetry
tracer = otel.Tracer(tracingName)
hook := otellogrus.NewHook(tracingName)
logger.AddHook(hook)
defer container.Terminate(ctx)
// -- -- Setup Tests
// -- Create Post to test with
validPost := models.Post{
BaseModel: models.BaseModel[models.PostID]{
ID: models.PostID(fmt.Sprintf("%025s", "Post1")),
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
DeletedAt: gorm.DeletedAt{},
},
Rating: models.SFW,
}
validPost, err = CreatePost(ctx, validPost)
if err != nil {
t.Fatalf("CreatePostReport err: %v", err)
}
// Setup Tests
validUser01 := models.User{BaseModel: models.BaseModel[models.UserID]{ID: models.UserID(fmt.Sprintf("%025s", "User1"))}}
validUser01, err = CreateUser(ctx, validUser01)
if err != nil {
t.Fatalf("CreatePostReport err: %v", err)
}
validUser02 := models.User{BaseModel: models.BaseModel[models.UserID]{ID: models.UserID(fmt.Sprintf("%025s", "User2"))}}
validUser02, err = CreateUser(ctx, validUser02)
if err != nil {
t.Fatalf("CreatePostReport err: %v", err)
}
// -- Create PostReport to test with
validPostReport := models.PostReport{
BaseModel: models.BaseModel[models.PostReportID]{
ID: models.PostReportID(fmt.Sprintf("%025s", "PostReport1")),
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
DeletedAt: gorm.DeletedAt{},
},
PostID: validPost.ID,
ReportBy: validUser01.ID,
ReportDescription: "aaa",
AuditBy: nil,
AuditDescription: "",
ReportType: models.Duplicate,
ReportState: models.Rejected,
}
// --
validPostReport, err = CreatePostReport(ctx, validPostReport)
if err != nil {
t.Fatalf("CreatePostReport err: %v", err)
}
// --
// --
// -- -- Tests
type args struct {
ctx context.Context
id models.PostReportID
}
tests := []struct {
name string
args args
wantErr bool
}{
{
name: "Test 01: Delete Valid PostReport",
args: args{
ctx: ctx,
id: validPostReport.ID,
},
wantErr: false,
},
{
name: "Test 02: Delete not existed PostReport",
args: args{
ctx: ctx,
id: validPostReport.ID,
},
wantErr: false,
},
{
name: "Test 03: Empty PostReportID",
args: args{
ctx: ctx,
id: "",
},
wantErr: true,
},
{
name: "Test 04: Short PostReportID",
args: args{
ctx: ctx,
id: "111",
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := DeletePostReport(tt.args.ctx, tt.args.id); (err != nil) != tt.wantErr {
t.Errorf("DeletePostReport() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
func checkPostReportID(got models.PostReport, want models.PostReport) bool {
if got.ID != want.ID {
return false
}
return true
}

View File

@ -7,8 +7,8 @@ import (
"testing"
"time"
"git.anthrove.art/Anthrove/otter-space-sdk/v4/pkg/models"
"git.anthrove.art/Anthrove/otter-space-sdk/v4/test"
"git.anthrove.art/Anthrove/otter-space-sdk/v5/pkg/models"
"git.anthrove.art/Anthrove/otter-space-sdk/v5/test"
"go.opentelemetry.io/contrib/bridges/otellogrus"
"go.opentelemetry.io/otel"
"gorm.io/gorm"

View File

@ -3,7 +3,7 @@ package database
import (
"math"
"git.anthrove.art/Anthrove/otter-space-sdk/v4/pkg/models"
"git.anthrove.art/Anthrove/otter-space-sdk/v5/pkg/models"
"gorm.io/gorm"
)

View File

@ -5,8 +5,8 @@ import (
"reflect"
"testing"
"git.anthrove.art/Anthrove/otter-space-sdk/v4/pkg/models"
"git.anthrove.art/Anthrove/otter-space-sdk/v4/test"
"git.anthrove.art/Anthrove/otter-space-sdk/v5/pkg/models"
"git.anthrove.art/Anthrove/otter-space-sdk/v5/test"
"go.opentelemetry.io/contrib/bridges/otellogrus"
"go.opentelemetry.io/otel"
"gorm.io/gorm"

View File

@ -4,9 +4,9 @@ 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"
"git.anthrove.art/Anthrove/otter-space-sdk/v5/internal/utils"
otterError "git.anthrove.art/Anthrove/otter-space-sdk/v5/pkg/error"
"git.anthrove.art/Anthrove/otter-space-sdk/v5/pkg/models"
log "github.com/sirupsen/logrus"
"go.opentelemetry.io/otel/attribute"
"gorm.io/gorm"

View File

@ -6,8 +6,8 @@ import (
"testing"
"time"
"git.anthrove.art/Anthrove/otter-space-sdk/v4/pkg/models"
"git.anthrove.art/Anthrove/otter-space-sdk/v4/test"
"git.anthrove.art/Anthrove/otter-space-sdk/v5/pkg/models"
"git.anthrove.art/Anthrove/otter-space-sdk/v5/test"
"go.opentelemetry.io/contrib/bridges/otellogrus"
"go.opentelemetry.io/otel"
"gorm.io/gorm"

View File

@ -4,9 +4,9 @@ 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"
"git.anthrove.art/Anthrove/otter-space-sdk/v5/internal/utils"
otterError "git.anthrove.art/Anthrove/otter-space-sdk/v5/pkg/error"
"git.anthrove.art/Anthrove/otter-space-sdk/v5/pkg/models"
log "github.com/sirupsen/logrus"
"go.opentelemetry.io/otel/attribute"
"gorm.io/gorm"

View File

@ -7,8 +7,8 @@ import (
"testing"
"time"
"git.anthrove.art/Anthrove/otter-space-sdk/v4/pkg/models"
"git.anthrove.art/Anthrove/otter-space-sdk/v4/test"
"git.anthrove.art/Anthrove/otter-space-sdk/v5/pkg/models"
"git.anthrove.art/Anthrove/otter-space-sdk/v5/test"
"go.opentelemetry.io/contrib/bridges/otellogrus"
"go.opentelemetry.io/otel"
"gorm.io/gorm"

View File

@ -4,9 +4,9 @@ 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"
"git.anthrove.art/Anthrove/otter-space-sdk/v5/internal/utils"
otterError "git.anthrove.art/Anthrove/otter-space-sdk/v5/pkg/error"
"git.anthrove.art/Anthrove/otter-space-sdk/v5/pkg/models"
log "github.com/sirupsen/logrus"
"go.opentelemetry.io/otel/attribute"
"gorm.io/gorm"

View File

@ -4,9 +4,9 @@ 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"
"git.anthrove.art/Anthrove/otter-space-sdk/v5/internal/utils"
otterError "git.anthrove.art/Anthrove/otter-space-sdk/v5/pkg/error"
"git.anthrove.art/Anthrove/otter-space-sdk/v5/pkg/models"
log "github.com/sirupsen/logrus"
"go.opentelemetry.io/otel/attribute"
"gorm.io/gorm"

View File

@ -5,8 +5,8 @@ import (
"reflect"
"testing"
"git.anthrove.art/Anthrove/otter-space-sdk/v4/pkg/models"
"git.anthrove.art/Anthrove/otter-space-sdk/v4/test"
"git.anthrove.art/Anthrove/otter-space-sdk/v5/pkg/models"
"git.anthrove.art/Anthrove/otter-space-sdk/v5/test"
"go.opentelemetry.io/contrib/bridges/otellogrus"
"go.opentelemetry.io/otel"
)

View File

@ -4,9 +4,9 @@ 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"
"git.anthrove.art/Anthrove/otter-space-sdk/v5/internal/utils"
otterError "git.anthrove.art/Anthrove/otter-space-sdk/v5/pkg/error"
"git.anthrove.art/Anthrove/otter-space-sdk/v5/pkg/models"
log "github.com/sirupsen/logrus"
"go.opentelemetry.io/otel/attribute"
"gorm.io/gorm"

View File

@ -5,8 +5,8 @@ import (
"reflect"
"testing"
"git.anthrove.art/Anthrove/otter-space-sdk/v4/pkg/models"
"git.anthrove.art/Anthrove/otter-space-sdk/v4/test"
"git.anthrove.art/Anthrove/otter-space-sdk/v5/pkg/models"
"git.anthrove.art/Anthrove/otter-space-sdk/v5/test"
"go.opentelemetry.io/contrib/bridges/otellogrus"
"go.opentelemetry.io/otel"
)

View File

@ -5,8 +5,8 @@ import (
"reflect"
"testing"
"git.anthrove.art/Anthrove/otter-space-sdk/v4/pkg/models"
"git.anthrove.art/Anthrove/otter-space-sdk/v4/test"
"git.anthrove.art/Anthrove/otter-space-sdk/v5/pkg/models"
"git.anthrove.art/Anthrove/otter-space-sdk/v5/test"
"go.opentelemetry.io/contrib/bridges/otellogrus"
"go.opentelemetry.io/otel"
)

View File

@ -4,9 +4,9 @@ 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"
"git.anthrove.art/Anthrove/otter-space-sdk/v5/internal/utils"
otterError "git.anthrove.art/Anthrove/otter-space-sdk/v5/pkg/error"
"git.anthrove.art/Anthrove/otter-space-sdk/v5/pkg/models"
log "github.com/sirupsen/logrus"
"go.opentelemetry.io/otel/attribute"
"gorm.io/gorm"

View File

@ -4,9 +4,9 @@ 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"
"git.anthrove.art/Anthrove/otter-space-sdk/v5/internal/utils"
otterError "git.anthrove.art/Anthrove/otter-space-sdk/v5/pkg/error"
"git.anthrove.art/Anthrove/otter-space-sdk/v5/pkg/models"
log "github.com/sirupsen/logrus"
"go.opentelemetry.io/otel/attribute"
"gorm.io/gorm"

View File

@ -7,8 +7,8 @@ import (
"testing"
"time"
"git.anthrove.art/Anthrove/otter-space-sdk/v4/pkg/models"
"git.anthrove.art/Anthrove/otter-space-sdk/v4/test"
"git.anthrove.art/Anthrove/otter-space-sdk/v5/pkg/models"
"git.anthrove.art/Anthrove/otter-space-sdk/v5/test"
"go.opentelemetry.io/contrib/bridges/otellogrus"
"go.opentelemetry.io/otel"
"gorm.io/gorm"

View File

@ -5,8 +5,8 @@ import (
"fmt"
"testing"
"git.anthrove.art/Anthrove/otter-space-sdk/v4/pkg/models"
"git.anthrove.art/Anthrove/otter-space-sdk/v4/test"
"git.anthrove.art/Anthrove/otter-space-sdk/v5/pkg/models"
"git.anthrove.art/Anthrove/otter-space-sdk/v5/test"
"go.opentelemetry.io/contrib/bridges/otellogrus"
"go.opentelemetry.io/otel"
)

View File

@ -26,7 +26,7 @@ const (
UserFavoriteIsWrongLength = "userFavoriteID has the wrong length"
PostListIsEmpty = "userFavoriteList cannot be empty"
PostIDIsEmpty = "userFavoriteID cannot be empty"
PostIDIsEmpty = "PostID cannot be empty"
PostIDIsWrongLength = "PostID has the wrong length"
BatchSizeIsEmpty = "batchSize cannot be empty"
@ -38,6 +38,9 @@ const (
ScrapeTaskIDIsEmpty = "ScrapeTaskID cannot be empty"
ScrapeTaskIDIsWrongLength = "ScrapeTaskID has the wrong length, needs to be 25 characters long"
PostReportIDIsEmpty = "PostReportID cannot be empty"
PostReportIDIsWrongLength = "PostReportID has the wrong length"
)
type EntityValidationFailed struct {

View File

@ -9,4 +9,5 @@ type DatabaseConfig struct {
SSL bool `env:"DB_SSL,required" envDefault:"true"`
Timezone string `env:"DB_TIMEZONE,required" envDefault:"Europe/Berlin"`
Debug bool `env:"DB_DEBUG" envDefault:"false"`
Migrate bool
}

View File

@ -25,6 +25,10 @@ type (
UserFavoriteID string
ScrapeTaskID string
PostReportID string
ReportType string
ReportState string
)
const (
@ -50,6 +54,21 @@ const (
Copyright TagType = "copyright"
)
const (
Duplicate ReportType = "duplicate"
MissingData ReportType = "missing_data"
RatingAbuse ReportType = "rating_abuse"
IllegalContent ReportType = "illegal_content"
)
const (
PendingUnclaimed ReportState = "pending_unclaimed"
Pending ReportState = "pending"
Approved ReportState = "approved"
Partial ReportState = "partial"
Rejected ReportState = "rejected"
)
func (r *Rating) Convert(e621Rating string) {
switch e621Rating {
case "e":

View File

@ -8,7 +8,7 @@ import (
)
type ID interface {
UserID | SourceID | PostID | UserSourceID | UserFavoriteID | PoolID
UserID | SourceID | PostID | UserSourceID | UserFavoriteID | PoolID | PostReportID
}
type BaseModel[T ID] struct {

View File

@ -9,8 +9,10 @@ const (
type Pool struct {
BaseModel[PoolID]
Name string `json:"name" gorm:"type:varchar(25)"`
Category PoolCategory `json:"category" gorm:"type:pool_category;type:enum('series', 'collection')"`
Name string `json:"name" gorm:"column:name;type:varchar(25)"`
Category PoolCategory `json:"category" gorm:"column:category;type:pool_category;type:enum('series', 'collection')"`
Posts []PoolPost `json:"posts" gorm:"foreignKey:PoolID"`
Sources []PoolReference `json:"sources" gorm:"foreignKey:PoolID"`
}
func (Pool) TableName() string {
@ -18,11 +20,10 @@ func (Pool) TableName() string {
}
type PoolPost struct {
Pool Pool `json:"pool" gorm:"foreignKey:ID;references:PoolID"`
PoolID PoolID `json:"pool_id" gorm:""`
Post Post `json:"post" gorm:"foreignKey:ID;references:PostID"`
PostID PostID `json:"post_id" gorm:""`
OrderPosition int `json:"order_position" gorm:"not null;default:0"`
PoolID PoolID `json:"pool_id" gorm:"column:pool_id;primaryKey"`
Pool Pool `json:"-"`
PostID PostID `json:"post_id" gorm:"column:post_id;primaryKey"`
OrderPosition int `json:"order_position" gorm:"column:order_position;default:0"`
}
func (PoolPost) TableName() string {
@ -30,11 +31,10 @@ func (PoolPost) TableName() string {
}
type PoolReference struct {
Pool Pool `json:"pool" gorm:"foreignKey:ID;references:PoolID"`
PoolID PoolID `json:"pool_id" gorm:""`
Source Source `json:"source" gorm:"foreignKey:ID;references:SourceID"`
SourceID SourceID `json:"source_id" gorm:""`
URL string `json:"url" gorm:"not null"`
PoolID PoolID `json:"pool_id" gorm:"column:pool_id;primaryKey"`
Pool Pool `json:"-"`
SourceID SourceID `json:"source_id" gorm:"column:source_id;primaryKey"`
URL string `json:"url" gorm:"column:url;primaryKey"`
}
func (PoolReference) TableName() string {

16
pkg/models/postReport.go Normal file
View File

@ -0,0 +1,16 @@
package models
type PostReport struct {
BaseModel[PostReportID]
PostID PostID `json:"post_id"`
ReportBy UserID `json:"report_by"`
ReportDescription string `json:"report_description"`
AuditBy *UserID `json:"audit_by"`
AuditDescription string `json:"audit_description"`
ReportType ReportType `json:"report_type" gorm:"type:enum('duplicate','missing_data','rating_abuse','illegal_content')"`
ReportState ReportState `json:"report_state" gorm:"type:enum('pending_unclaimed', 'pending', 'approved', 'partial', 'rejected')"`
}
func (PostReport) TableName() string {
return "PostReport"
}

View File

@ -0,0 +1,11 @@
package models
import "testing"
func TestPostReport_TableName(t *testing.T) {
postReport := PostReport{}
expectedTableName := "PostReport"
if tableName := postReport.TableName(); tableName != expectedTableName {
t.Fatalf("expected %s, but got %s", expectedTableName, tableName)
}
}

View File

@ -5,7 +5,7 @@ import (
"math/rand"
"time"
"git.anthrove.art/Anthrove/otter-space-sdk/v4/pkg/models"
"git.anthrove.art/Anthrove/otter-space-sdk/v5/pkg/models"
"github.com/davecgh/go-spew/spew"
gonanoid "github.com/matoous/go-nanoid/v2"
"gorm.io/gorm"

View File

@ -8,7 +8,7 @@ import (
"strings"
"time"
"git.anthrove.art/Anthrove/otter-space-sdk/v4/pkg/models"
"git.anthrove.art/Anthrove/otter-space-sdk/v5/pkg/models"
migrate "github.com/rubenv/sql-migrate"
postgrescontainer "github.com/testcontainers/testcontainers-go/modules/postgres"
"gorm.io/driver/postgres"
@ -121,6 +121,7 @@ func DatabaseModesFromConnectionString(ctx context.Context, pgContainer *postgre
SSL: false,
Timezone: "Europe/Berlin",
Debug: true,
Migrate: true,
}
return databaseConfig, nil