From 64439d9041b8936c966183e0020913610b80735c Mon Sep 17 00:00:00 2001 From: John Costa Date: Sat, 10 May 2025 16:03:31 +0100 Subject: [PATCH] feat: using struct to send notification and improved image model --- backend/events.go | 22 ++++++++++++++++------ backend/main.go | 4 ++-- backend/models/image.go | 22 ---------------------- 3 files changed, 18 insertions(+), 30 deletions(-) diff --git a/backend/events.go b/backend/events.go index b4b20b7..4c021cb 100644 --- a/backend/events.go +++ b/backend/events.go @@ -15,7 +15,12 @@ import ( "github.com/lib/pq" ) -func ListenNewImageEvents(db *sql.DB, notifier *Notifier[string]) { +type Notification struct { + ImageID uuid.UUID + Status string +} + +func ListenNewImageEvents(db *sql.DB, notifier *Notifier[Notification]) { listener := pq.NewListener(os.Getenv("DB_CONNECTION"), time.Second, time.Second, func(event pq.ListenerEventType, err error) { if err != nil { panic(err) @@ -79,7 +84,7 @@ func ListenNewImageEvents(db *sql.DB, notifier *Notifier[string]) { } } -func ListenProcessingImageStatus(db *sql.DB, notifier *Notifier[string]) { +func ListenProcessingImageStatus(db *sql.DB, images models.ImageModel, notifier *Notifier[Notification]) { listener := pq.NewListener(os.Getenv("DB_CONNECTION"), time.Second, time.Second, func(event pq.ListenerEventType, err error) { if err != nil { panic(err) @@ -105,22 +110,27 @@ func ListenProcessingImageStatus(db *sql.DB, notifier *Notifier[string]) { continue } - userId, err := models.GetUserId(db, context.Background(), imageUuid) + processingImage, err := images.GetToProcess(context.Background(), imageUuid) if err != nil { - logger.Error("GetUserID failed", "err", err) + logger.Error("GetToProcess failed", "err", err) continue } logger.Info("Update", "id", imageStringUuid, "status", status) - if err := notifier.SendAndCreate(userId.String(), status); err != nil { + notification := Notification{ + ImageID: processingImage.ImageID, + Status: status, + } + + if err := notifier.SendAndCreate(processingImage.UserID.String(), notification); err != nil { logger.Error(err) } } } } -func CreateEventsHandler(notifier *Notifier[string]) http.HandlerFunc { +func CreateEventsHandler(notifier *Notifier[Notification]) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { userId := r.Context().Value(USER_ID).(uuid.UUID) if userId == uuid.Nil { diff --git a/backend/main.go b/backend/main.go index 6c5da7d..6dcc2c5 100644 --- a/backend/main.go +++ b/backend/main.go @@ -48,10 +48,10 @@ func main() { auth := CreateAuth(mail) - notifier := NewNotifier[string](10) + notifier := NewNotifier[Notification](10) go ListenNewImageEvents(db, ¬ifier) - go ListenProcessingImageStatus(db, ¬ifier) + go ListenProcessingImageStatus(db, imageModel, ¬ifier) r := chi.NewRouter() diff --git a/backend/models/image.go b/backend/models/image.go index 2fe300f..1406ba2 100644 --- a/backend/models/image.go +++ b/backend/models/image.go @@ -180,28 +180,6 @@ func (m ImageModel) IsUserAuthorized(ctx context.Context, imageId uuid.UUID, use return err != nil && userImage.UserID.String() == userId.String() } -func GetUserId(dbPool *sql.DB, ctx context.Context, imageId uuid.UUID) (uuid.UUID, error) { - getUserIdStmt := UserImagesToProcess. - SELECT(UserImagesToProcess.UserID). - WHERE(UserImagesToProcess.ID.EQ(UUID(imageId))) - - userImage := model.UserImagesToProcess{} - err := getUserIdStmt.QueryContext(ctx, dbPool, &userImage) - - return userImage.UserID, err -} - -func GetUserIdComplete(dbPool *sql.DB, ctx context.Context, imageId uuid.UUID) (uuid.UUID, error) { - getUserIdStmt := UserImages. - SELECT(UserImages.UserID). - WHERE(UserImages.ID.EQ(UUID(imageId))) - - userImage := model.UserImagesToProcess{} - err := getUserIdStmt.QueryContext(ctx, dbPool, &userImage) - - return userImage.UserID, err -} - func NewImageModel(db *sql.DB) ImageModel { return ImageModel{dbPool: db} }