Skip to content

Commit

Permalink
[FIX] Fixed the image and document summary
Browse files Browse the repository at this point in the history
  • Loading branch information
pathfindermilan committed Nov 4, 2024
1 parent d418719 commit 05b0956
Show file tree
Hide file tree
Showing 8 changed files with 194 additions and 121 deletions.
33 changes: 20 additions & 13 deletions backend/internal/controllers/sync_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@ type SyncController struct {
syncDescriptionService services.SyncDescriptionService
}

func NewSyncController(
syncService services.SyncService,
syncDescriptionService services.SyncDescriptionService,
) *SyncController {
func NewSyncController(syncService services.SyncService, syncDescriptionService services.SyncDescriptionService) *SyncController {
return &SyncController{
syncService: syncService,
syncDescriptionService: syncDescriptionService,
Expand Down Expand Up @@ -114,6 +111,9 @@ func (ctrl *SyncController) SyncData(c *gin.Context) {
return
}

newImageUploaded := false
newDocumentUploaded := false

if imageFile != nil {
mimeType := imageFile.Header.Get("Content-Type")
if !allowedImageTypes[mimeType] {
Expand All @@ -126,9 +126,14 @@ func (ctrl *SyncController) SyncData(c *gin.Context) {
uploadImage = true
if existingSyncData != nil && existingSyncData.ImageURL != "" {
existingImageName := filepath.Base(existingSyncData.ImageURL)
if strings.EqualFold(cleanImageFilename, existingImageName) {
if !strings.EqualFold(cleanImageFilename, existingImageName) {
newImageUploaded = true
uploadImage = true
} else {
uploadImage = false
}
} else {
newImageUploaded = true
}

if uploadImage {
Expand All @@ -138,7 +143,7 @@ func (ctrl *SyncController) SyncData(c *gin.Context) {
}
}

newImageFilename := strconv.Itoa(int(userID)) + "-" + imageFile.Filename
newImageFilename := strconv.Itoa(int(userID)) + "-" + cleanImageFilename
imagePath := filepath.Join("uploads/images", newImageFilename)

if err := os.MkdirAll(filepath.Dir(imagePath), os.ModePerm); err != nil {
Expand Down Expand Up @@ -172,9 +177,14 @@ func (ctrl *SyncController) SyncData(c *gin.Context) {
uploadDocument = true
if existingSyncData != nil && existingSyncData.DocumentURL != "" {
existingDocumentName := filepath.Base(existingSyncData.DocumentURL)
if strings.EqualFold(cleanDocumentFilename, existingDocumentName) {
if !strings.EqualFold(cleanDocumentFilename, existingDocumentName) {
newDocumentUploaded = true
uploadDocument = true
} else {
uploadDocument = false
}
} else {
newDocumentUploaded = true
}

if uploadDocument {
Expand All @@ -184,7 +194,7 @@ func (ctrl *SyncController) SyncData(c *gin.Context) {
}
}

newDocumentFilename := strconv.Itoa(int(userID)) + "-" + documentFile.Filename
newDocumentFilename := strconv.Itoa(int(userID)) + "-" + cleanDocumentFilename
documentPath := filepath.Join("uploads/documents", newDocumentFilename)

if err := os.MkdirAll(filepath.Dir(documentPath), os.ModePerm); err != nil {
Expand Down Expand Up @@ -216,16 +226,13 @@ func (ctrl *SyncController) SyncData(c *gin.Context) {
DocumentURL: documentURL,
}

newImageUploaded := (imageFile != nil && uploadImage)
newDocumentUploaded := (documentFile != nil && uploadDocument)

err = ctrl.syncService.UpsertSyncData(&syncData)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to save sync data"})
return
}

go ctrl.syncService.ProcessDescriptions(userID, newImageUploaded, newDocumentUploaded)
ctrl.syncService.ProcessDescriptions(userID, newImageUploaded, newDocumentUploaded)

c.JSON(http.StatusOK, gin.H{"message": "Data synced successfully"})
}
Expand Down Expand Up @@ -268,7 +275,7 @@ func (ctrl *SyncController) SyncReset(c *gin.Context) {
return
}

err = ctrl.syncDescriptionService.DeleteSyncDescription(userID)
err = ctrl.syncService.DeleteSyncDescription(userID) // Use SyncService method
if err != nil && !gorm.IsRecordNotFoundError(err) {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to delete sync description data"})
return
Expand Down
21 changes: 8 additions & 13 deletions backend/internal/models/sync_data.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
package models

import (
"github.com/jinzhu/gorm"
)

type SyncData struct {
gorm.Model
UserID uint `gorm:"unique;not null" json:"user_id"`
ContentType string `gorm:"not null"`
Age int `gorm:"not null"`
QueryText string `gorm:"not null"`
FeelingLevel string `gorm:"not null"` // happy, sad, angry, confused, confident, tired

ImageURL string
DocumentURL string
ID uint `gorm:"primary_key"`
UserID uint `gorm:"uniqueIndex"`
ContentType string
Age int
QueryText string
FeelingLevel string
ImageURL string
DocumentURL string
}
27 changes: 9 additions & 18 deletions backend/internal/models/sync_description.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,13 @@
package models

import "github.com/jinzhu/gorm"

type SyncStatus string

const (
StatusInProgress SyncStatus = "inProgress"
StatusDone SyncStatus = "done"
StatusErrored SyncStatus = "errored"
StatusNotFound SyncStatus = "notFound"
)

type SyncDescription struct {
gorm.Model
UserID uint `gorm:"not null"`
SyncDataID uint `gorm:"not null"`
ImageSummary string `gorm:"type:text"`
DocumentSummary string `gorm:"type:text"`
ImageStatus SyncStatus `gorm:"not null"`
DocumentStatus SyncStatus `gorm:"not null"`
ID uint `gorm:"primary_key"`
UserID uint `gorm:"uniqueIndex"`
SyncDataID uint
ImageURL string
ImageSummary string
ImageStatus SyncStatus
DocumentURL string
DocumentSummary string
DocumentStatus SyncStatus
}
10 changes: 10 additions & 0 deletions backend/internal/models/sync_status.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package models

type SyncStatus string

const (
StatusInProgress SyncStatus = "InProgress"
StatusDone SyncStatus = "Done"
StatusErrored SyncStatus = "Errored"
StatusNotFound SyncStatus = "NotFound"
)
45 changes: 24 additions & 21 deletions backend/internal/repositories/sync_description_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,38 +7,41 @@ import (
)

type SyncDescriptionRepository interface {
CreateOrUpdateSyncDescription(data *models.SyncDescription) error
GetSyncDescriptionByUserID(userID uint) (*models.SyncDescription, error)
DeleteSyncDescription(userID uint) error
GetSyncDescriptionByUserID(userID uint) (*models.SyncDescription, error)
CreateOrUpdateSyncDescription(desc *models.SyncDescription) error
DeleteSyncDescription(userID uint) error
}

type syncDescriptionRepository struct {
db *gorm.DB
db *gorm.DB
}

func NewSyncDescriptionRepository(db *gorm.DB) SyncDescriptionRepository {
return &syncDescriptionRepository{db}
return &syncDescriptionRepository{db}
}

func (r *syncDescriptionRepository) CreateOrUpdateSyncDescription(data *models.SyncDescription) error {
var existing models.SyncDescription
err := r.db.Where("user_id = ?", data.UserID).First(&existing).Error
if err != nil && !gorm.IsRecordNotFoundError(err) {
return err
}
if existing.ID != 0 {
data.ID = existing.ID
return r.db.Save(data).Error
}
return r.db.Create(data).Error
func (r *syncDescriptionRepository) GetSyncDescriptionByUserID(userID uint) (*models.SyncDescription, error) {
var desc models.SyncDescription
err := r.db.Where("user_id = ?", userID).First(&desc).Error
if err != nil {
return nil, err
}
return &desc, nil
}

func (r *syncDescriptionRepository) GetSyncDescriptionByUserID(userID uint) (*models.SyncDescription, error) {
var data models.SyncDescription
err := r.db.Where("user_id = ?", userID).First(&data).Error
return &data, err
func (r *syncDescriptionRepository) CreateOrUpdateSyncDescription(desc *models.SyncDescription) error {
var existing models.SyncDescription
err := r.db.Where("user_id = ?", desc.UserID).First(&existing).Error
if err != nil && !gorm.IsRecordNotFoundError(err) {
return err
}
if existing.ID != 0 {
desc.ID = existing.ID
return r.db.Save(desc).Error
}
return r.db.Create(desc).Error
}

func (r *syncDescriptionRepository) DeleteSyncDescription(userID uint) error {
return r.db.Where("user_id = ?", userID).Delete(&models.SyncDescription{}).Error
return r.db.Where("user_id = ?", userID).Delete(&models.SyncDescription{}).Error
}
31 changes: 9 additions & 22 deletions backend/internal/repositories/sync_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,33 +26,20 @@ func (r *syncRepository) CreateSyncData(data *models.SyncData) error {
}

func (r *syncRepository) UpsertSyncData(data *models.SyncData) error {
var existingData models.SyncData
err := r.db.Where("user_id = ?", data.UserID).First(&existingData).Error
if err != nil {
if gorm.IsRecordNotFoundError(err) {
return r.db.Create(data).Error
}
var existing models.SyncData
err := r.db.Where("user_id = ?", data.UserID).First(&existing).Error
if err != nil && !gorm.IsRecordNotFoundError(err) {
return err
}
existingData.ContentType = data.ContentType
existingData.Age = data.Age
existingData.QueryText = data.QueryText
existingData.FeelingLevel = data.FeelingLevel
existingData.ImageURL = data.ImageURL
existingData.DocumentURL = data.DocumentURL

return r.db.Save(&existingData).Error
if existing.ID != 0 {
data.ID = existing.ID
return r.db.Save(data).Error
}
return r.db.Create(data).Error
}

func (r *syncRepository) DeleteSyncData(userID uint) error {
result := r.db.Where("user_id = ?", userID).Delete(&models.SyncData{})
if result.Error != nil {
return result.Error
}
if result.RowsAffected == 0 {
return gorm.ErrRecordNotFound
}
return nil
return r.db.Where("user_id = ?", userID).Delete(&models.SyncData{}).Error
}

func (r *syncRepository) GetSyncData(userID uint) (*models.SyncData, error) {
Expand Down
22 changes: 11 additions & 11 deletions backend/internal/services/sync_description_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,27 @@ import (
)

type SyncDescriptionService interface {
CreateOrUpdateSyncDescription(data *models.SyncDescription) error
GetSyncDescription(userID uint) (*models.SyncDescription, error)
DeleteSyncDescription(userID uint) error
GetSyncDescriptionByUserID(userID uint) (*models.SyncDescription, error)
CreateOrUpdateSyncDescription(desc *models.SyncDescription) error
DeleteSyncDescription(userID uint) error
}

type syncDescriptionService struct {
syncDescriptionRepo repositories.SyncDescriptionRepository
repo repositories.SyncDescriptionRepository
}

func NewSyncDescriptionService(syncDescriptionRepo repositories.SyncDescriptionRepository) SyncDescriptionService {
return &syncDescriptionService{syncDescriptionRepo}
func NewSyncDescriptionService(repo repositories.SyncDescriptionRepository) SyncDescriptionService {
return &syncDescriptionService{repo}
}

func (s *syncDescriptionService) CreateOrUpdateSyncDescription(data *models.SyncDescription) error {
return s.syncDescriptionRepo.CreateOrUpdateSyncDescription(data)
func (s *syncDescriptionService) GetSyncDescriptionByUserID(userID uint) (*models.SyncDescription, error) {
return s.repo.GetSyncDescriptionByUserID(userID)
}

func (s *syncDescriptionService) GetSyncDescription(userID uint) (*models.SyncDescription, error) {
return s.syncDescriptionRepo.GetSyncDescriptionByUserID(userID)
func (s *syncDescriptionService) CreateOrUpdateSyncDescription(desc *models.SyncDescription) error {
return s.repo.CreateOrUpdateSyncDescription(desc)
}

func (s *syncDescriptionService) DeleteSyncDescription(userID uint) error {
return s.syncDescriptionRepo.DeleteSyncDescription(userID)
return s.repo.DeleteSyncDescription(userID)
}
Loading

0 comments on commit 05b0956

Please sign in to comment.