feat: using struct to send notification and improved image model
This commit is contained in:
@ -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 {
|
||||
|
@ -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()
|
||||
|
||||
|
@ -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}
|
||||
}
|
||||
|
Reference in New Issue
Block a user