Skip to content

Commit

Permalink
[Backend] Include username/userid on marker apis
Browse files Browse the repository at this point in the history
  • Loading branch information
Alfex4936 committed Feb 22, 2024
1 parent a6c4e4b commit 103bcb5
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 12 deletions.
1 change: 1 addition & 0 deletions backend/dto/marker_dto.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ type MarkerResponse struct {
Longitude float64 `json:"longitude"`
Description string `json:"description"`
Username string `json:"username"`
UserID int `json:"userID"`
PhotoURLs []string `json:"photoUrls"`
}
1 change: 1 addition & 0 deletions backend/handlers/marker_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ func CreateMarkerWithPhotosHandler(c *fiber.Ctx) error {
}

marker.Username = username
marker.UserID = userId

return c.Status(fiber.StatusCreated).JSON(marker)
}
Expand Down
3 changes: 2 additions & 1 deletion backend/models/marker.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@ type MarkerWithPhoto struct {

type MarkerWithPhotos struct {
Marker
Photos []Photo `json:"photos,omitempty"`
Username string `json:"username,omitempty"`
Photos []Photo `json:"photos,omitempty"`
}
29 changes: 18 additions & 11 deletions backend/services/marker_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,30 +116,37 @@ func CreateMarkerWithPhotos(markerDto *dto.MarkerRequest, userID int, form *mult
}

func GetAllMarkers() ([]models.MarkerWithPhotos, error) {
// Query to select all markers
const markerQuery = `SELECT * FROM Markers`
// Assuming an updated query that joins Markers with Users to select the username as well
const markerQuery = `
SELECT Markers.*, Users.Username
FROM Markers
JOIN Users ON Markers.UserID = Users.UserID`

// Query to select photos for a marker
const photoQuery = `SELECT * FROM Photos WHERE MarkerID = ?`

// Slice to hold the results
var markers []models.Marker
err := database.DB.Select(&markers, markerQuery)
var markersWithUsernames []struct {
models.Marker
Username string `db:"Username"`
}
err := database.DB.Select(&markersWithUsernames, markerQuery)
if err != nil {
return nil, fmt.Errorf("fetching markers: %w", err)
return nil, fmt.Errorf("fetching markers with usernames: %w", err)
}

var markersWithPhotos []models.MarkerWithPhotos
for _, marker := range markers {
for _, markerWithUsername := range markersWithUsernames {
var photos []models.Photo
err := database.DB.Select(&photos, photoQuery, marker.MarkerID)
err := database.DB.Select(&photos, photoQuery, markerWithUsername.MarkerID)
if err != nil {
return nil, fmt.Errorf("fetching photos for marker %d: %w", marker.MarkerID, err)
return nil, fmt.Errorf("fetching photos for marker %d: %w", markerWithUsername.MarkerID, err)
}

// Adding Username to the MarkerWithPhotos
markersWithPhotos = append(markersWithPhotos, models.MarkerWithPhotos{
Marker: marker,
Photos: photos,
Marker: markerWithUsername.Marker, // Marker includes all previous fields
Photos: photos,
Username: markerWithUsername.Username, // Include the fetched Username
})
}

Expand Down

0 comments on commit 103bcb5

Please sign in to comment.