Skip to content

Commit

Permalink
[Backend] Fix placeholder on creating a marker
Browse files Browse the repository at this point in the history
  • Loading branch information
Alfex4936 committed Feb 20, 2024
1 parent eea50d9 commit c18a721
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 10 deletions.
2 changes: 1 addition & 1 deletion backend/handlers/comment_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

// PostComment creates a new comment
func PostComment(c *fiber.Ctx) error {
userID := c.Locals("userID").(int) // Extract userID from middleware
userID := c.Locals("userID").(int)
markerID, _ := strconv.Atoi(c.FormValue("markerId"))
commentText := c.FormValue("commentText")

Expand Down
16 changes: 7 additions & 9 deletions backend/services/marker_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,19 +63,17 @@ func CreateMarkerWithPhotos(markerDto *dto.MarkerRequest, userID int, form *mult
}

// Insert the marker into the database
res, err := tx.Exec("INSERT INTO Markers (UserID, Latitude, Longitude, Description, CreatedAt, UpdatedAt)", userID, markerDto.Latitude, markerDto.Longitude, markerDto.Description)
res, err := tx.Exec(
"INSERT INTO Markers (UserID, Latitude, Longitude, Description, CreatedAt, UpdatedAt) VALUES (?, ?, ?, ?, NOW(), NOW())",
userID, markerDto.Latitude, markerDto.Longitude, markerDto.Description,
)
if err != nil {
tx.Rollback()
return nil, err
}

markerID, _ := res.LastInsertId()

// Commit the transaction after successfully creating the marker
if err := tx.Commit(); err != nil {
return nil, err
}

// After successfully creating the marker, process and upload the files
var photoURLs []string

Expand All @@ -91,14 +89,14 @@ func CreateMarkerWithPhotos(markerDto *dto.MarkerRequest, userID int, form *mult
photoURLs = append(photoURLs, fileURL)

// Associate each photo with the marker in the database
// This operation is separate from the marker creation transaction
if _, err := database.DB.Exec("INSERT INTO Photos (MarkerID, PhotoURL, UploadedAt) VALUES (?, ?, NOW())", markerID, fileURL); err != nil {
fmt.Printf("Could not insert photo record: %v\n", err)
if _, err := tx.Exec("INSERT INTO Photos (MarkerID, PhotoURL, UploadedAt) VALUES (?, ?, NOW())", markerID, fileURL); err != nil {
tx.Rollback()

// Attempt to delete the uploaded file from S3
if delErr := DeleteDataFromS3(fileURL); delErr != nil {
fmt.Printf("Also failed to delete the file from S3: %v\n", delErr)
}
return nil, err
}
}

Expand Down

0 comments on commit c18a721

Please sign in to comment.