feat: using struct to send notification and improved image model

This commit is contained in:
2025-05-10 16:03:31 +01:00
parent 2f3d643278
commit 64439d9041
3 changed files with 18 additions and 30 deletions

View File

@ -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 {

View File

@ -48,10 +48,10 @@ func main() {
auth := CreateAuth(mail)
notifier := NewNotifier[string](10)
notifier := NewNotifier[Notification](10)
go ListenNewImageEvents(db, &notifier)
go ListenProcessingImageStatus(db, &notifier)
go ListenProcessingImageStatus(db, imageModel, &notifier)
r := chi.NewRouter()

View File

@ -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}
}