Skip to content

Commit

Permalink
[Backend] Get all markers by a user API
Browse files Browse the repository at this point in the history
Moved some to utils package
  • Loading branch information
Alfex4936 committed Mar 3, 2024
1 parent 1057b36 commit bbd3983
Show file tree
Hide file tree
Showing 10 changed files with 368 additions and 125 deletions.
115 changes: 114 additions & 1 deletion backend/docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ const docTemplate = `{
"tags": [
"auth"
],
"summary": "Sign up a new user",
"summary": "Sign up a new user [normal]",
"operationId": "sign-up-user",
"parameters": [
{
Expand Down Expand Up @@ -339,6 +339,89 @@ const docTemplate = `{
}
}
}
},
"/markers/close": {
"get": {
"security": [
{
"ApiKeyAuth": []
}
],
"description": "This endpoint retrieves markers that are close to a specified location within a given distance.\nIt requires latitude, longitude, distance, and the number of markers (N) to return.\nIf no markers are found within the specified distance, it returns a \"No markers found\" message.\nReturns a list of markers that meet the criteria. (maximum 3km distance allowed)",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"markers"
],
"summary": "Find close markers",
"operationId": "find-close-markers",
"parameters": [
{
"type": "number",
"description": "Latitude of the location (float)",
"name": "latitude",
"in": "query",
"required": true
},
{
"type": "number",
"description": "Longitude of the location (float)",
"name": "longitude",
"in": "query",
"required": true
},
{
"type": "integer",
"description": "Search radius distance (meters)",
"name": "distance",
"in": "query",
"required": true
},
{
"type": "integer",
"description": "Number of markers to return",
"name": "N",
"in": "query",
"required": true
}
],
"responses": {
"200": {
"description": "Markers found successfully (with distance)",
"schema": {
"type": "array",
"items": {
"$ref": "#/definitions/dto.MarkerWithDistance"
}
}
},
"400": {
"description": "Invalid query parameters",
"schema": {
"type": "object",
"additionalProperties": true
}
},
"404": {
"description": "No markers found within the specified distance",
"schema": {
"type": "object",
"additionalProperties": true
}
},
"500": {
"description": "Internal server error",
"schema": {
"type": "object",
"additionalProperties": true
}
}
}
}
}
},
"definitions": {
Expand All @@ -364,6 +447,36 @@ const docTemplate = `{
}
}
},
"dto.MarkerWithDistance": {
"type": "object",
"properties": {
"createdAt": {
"type": "string"
},
"description": {
"type": "string"
},
"distance": {
"description": "Distance in meters",
"type": "number"
},
"latitude": {
"type": "number"
},
"longitude": {
"type": "number"
},
"markerId": {
"type": "integer"
},
"updatedAt": {
"type": "string"
},
"userId": {
"type": "integer"
}
}
},
"dto.SignUpRequest": {
"type": "object",
"properties": {
Expand Down
62 changes: 45 additions & 17 deletions backend/handlers/marker_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,12 @@ package handlers
import (
"chulbong-kr/dto"
"chulbong-kr/services"
"chulbong-kr/utils"
"strconv"

"github.com/gofiber/fiber/v2"
)

// QueryParamsExample handler
func QueryParamsExample(c *fiber.Ctx) error {
// Capture query parameters
query1 := c.Query("query")
query2 := c.Query("query2")

// You can also provide a default value if a query parameter is missing
query3 := c.Query("query3", "default value")

return c.JSON(fiber.Map{
"query1": query1,
"query2": query2,
"query3": query3,
})
}

func CreateMarkerWithPhotosHandler(c *fiber.Ctx) error {
// Parse the multipart form
form, err := c.MultipartForm()
Expand All @@ -49,7 +34,7 @@ func CreateMarkerWithPhotosHandler(c *fiber.Ctx) error {
}

// Location Must Be Inside South Korea
yes := services.IsInSouthKorea(latitude, longitude)
yes := utils.IsInSouthKorea(latitude, longitude)
if !yes {
return c.Status(fiber.StatusForbidden).JSON(fiber.Map{"error": "Operation not allowed within South Korea."})
}
Expand Down Expand Up @@ -284,6 +269,24 @@ func UndoDislikeHandler(c *fiber.Ctx) error {
return c.SendStatus(fiber.StatusOK)
}

func GetUserMarkersHandler(c *fiber.Ctx) error {
// Retrieve userID from c.Locals, which is set during authentication
userID, ok := c.Locals("userID").(int)
if !ok {
// If userID is not available, return an error response
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "User not authenticated"})
}

// Call the modified service function with the userID
markersWithPhotos, err := services.GetAllMarkersByUser(userID)
if err != nil {
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"error": err.Error()})
}

// Return the filtered markers
return c.JSON(markersWithPhotos)
}

// CheckDislikeStatus handler
func CheckDislikeStatus(c *fiber.Ctx) error {
userID := c.Locals("userID").(int)
Expand All @@ -300,12 +303,37 @@ func CheckDislikeStatus(c *fiber.Ctx) error {
return c.JSON(fiber.Map{"disliked": disliked})
}

// Find Close Markers godoc
//
// @Summary Find close markers
// @Description This endpoint retrieves markers that are close to a specified location within a given distance.
// @Description It requires latitude, longitude, distance, and the number of markers (N) to return.
// @Description If no markers are found within the specified distance, it returns a "No markers found" message.
// @Description Returns a list of markers that meet the criteria. (maximum 3km distance allowed)
// @ID find-close-markers
// @Tags markers
// @Accept json
// @Produce json
// @Param latitude query number true "Latitude of the location (float)"
// @Param longitude query number true "Longitude of the location (float)"
// @Param distance query int true "Search radius distance (meters)"
// @Param N query int true "Number of markers to return"
// @Security ApiKeyAuth
// @Success 200 {array} dto.MarkerWithDistance "Markers found successfully (with distance)"
// @Failure 400 {object} map[string]interface{} "Invalid query parameters"
// @Failure 404 {object} map[string]interface{} "No markers found within the specified distance"
// @Failure 500 {object} map[string]interface{} "Internal server error"
// @Router /markers/close [get]
func FindCloseMarkersHandler(c *fiber.Ctx) error {
var params dto.QueryParams
if err := c.QueryParser(&params); err != nil {
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "Invalid query parameters"})
}

if params.Distance > 3000 {
return c.Status(fiber.StatusForbidden).JSON(fiber.Map{"error": "Distance cannot be greater than 3,000m (3km)"})
}

// Find nearby markers within the specified distance
markers, err := services.FindClosestNMarkersWithinDistance(params.Latitude, params.Longitude, params.Distance, params.N)
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion backend/handlers/oauth_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package handlers
import (
"chulbong-kr/dto"
"chulbong-kr/services"
"chulbong-kr/utils"
"fmt"
"os"

Expand All @@ -14,7 +15,7 @@ import (
func GetGoogleAuthHandler(conf *oauth2.Config) fiber.Handler {
return func(c *fiber.Ctx) error {
// Generate a state string for CSRF protection
state := services.GenerateState()
state := utils.GenerateState()
c.Cookie(&fiber.Cookie{
Name: "oauthstate",
Value: state,
Expand Down
9 changes: 5 additions & 4 deletions backend/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,15 +148,16 @@ func main() {
markerGroup := api.Group("/markers")
{
markerGroup.Use(middlewares.AuthMiddleware)
markerGroup.Post("/new", handlers.CreateMarkerWithPhotosHandler)
markerGroup.Get("/my", handlers.GetUserMarkersHandler)
// markerGroup.Get("/:markerID", handlers.GetMarker)
markerGroup.Get("/close", handlers.FindCloseMarkersHandler)
markerGroup.Get("/:markerID/dislike-status", handlers.CheckDislikeStatus)
markerGroup.Put("/:markerID", handlers.UpdateMarker)
markerGroup.Post("/new", handlers.CreateMarkerWithPhotosHandler)
markerGroup.Post("/upload", handlers.UploadMarkerPhotoToS3Handler)
markerGroup.Delete("/:markerID", handlers.DeleteMarkerHandler)
markerGroup.Post("/:markerID/dislike", handlers.LeaveDislikeHandler)
markerGroup.Put("/:markerID", handlers.UpdateMarker)
markerGroup.Delete("/:markerID", handlers.DeleteMarkerHandler)
markerGroup.Delete("/:markerID/dislike", handlers.UndoDislikeHandler)
markerGroup.Get("/close", handlers.FindCloseMarkersHandler)
}

// Comment routes
Expand Down
Loading

0 comments on commit bbd3983

Please sign in to comment.