This commit is contained in:
@@ -1 +1,122 @@
|
||||
package controllers
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"context"
|
||||
"history-api/internal/dtos/request"
|
||||
"history-api/internal/dtos/response"
|
||||
"history-api/internal/services"
|
||||
"history-api/pkg/validator"
|
||||
"time"
|
||||
|
||||
"github.com/gofiber/fiber/v3"
|
||||
)
|
||||
|
||||
type AuthController struct {
|
||||
service services.AuthService
|
||||
}
|
||||
|
||||
func NewAuthController(svc services.AuthService) *AuthController {
|
||||
return &AuthController{service: svc}
|
||||
}
|
||||
|
||||
// Signin godoc
|
||||
// @Summary Sign in an existing user
|
||||
// @Description Authenticate user and return token data
|
||||
// @Tags Auth
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param request body request.SignInDto true "Sign In request"
|
||||
// @Success 200 {object} response.CommonResponse
|
||||
// @Failure 400 {object} response.CommonResponse
|
||||
// @Failure 500 {object} response.CommonResponse
|
||||
// @Router /auth/signin [post]
|
||||
func (h *AuthController) Signin(c fiber.Ctx) error {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
defer cancel()
|
||||
dto := &request.SignInDto{}
|
||||
|
||||
if err := validator.ValidateBodyDto(c, dto); err != nil {
|
||||
return c.Status(fiber.StatusBadRequest).JSON(response.CommonResponse{
|
||||
Status: false,
|
||||
Message: err.Error(),
|
||||
})
|
||||
}
|
||||
|
||||
res, err := h.service.Signin(ctx, dto)
|
||||
if err != nil {
|
||||
return c.Status(fiber.StatusInternalServerError).JSON(response.CommonResponse{
|
||||
Status: false,
|
||||
Message: err.Error(),
|
||||
})
|
||||
}
|
||||
|
||||
return c.Status(fiber.StatusOK).JSON(response.CommonResponse{
|
||||
Status: true,
|
||||
Data: res,
|
||||
})
|
||||
}
|
||||
|
||||
// Signup godoc
|
||||
// @Summary Sign up a new user
|
||||
// @Description Create a new user account
|
||||
// @Tags Auth
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param request body request.SignUpDto true "Sign Up request"
|
||||
// @Success 200 {object} response.CommonResponse
|
||||
// @Failure 400 {object} response.CommonResponse
|
||||
// @Failure 500 {object} response.CommonResponse
|
||||
// @Router /auth/signup [post]
|
||||
func (h *AuthController) Signup(c fiber.Ctx) error {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
defer cancel()
|
||||
dto := &request.SignUpDto{}
|
||||
|
||||
if err := validator.ValidateBodyDto(c, dto); err != nil {
|
||||
return c.Status(fiber.StatusBadRequest).JSON(response.CommonResponse{
|
||||
Status: false,
|
||||
Message: err.Error(),
|
||||
})
|
||||
}
|
||||
|
||||
res, err := h.service.Signup(ctx, dto)
|
||||
if err != nil {
|
||||
return c.Status(fiber.StatusInternalServerError).JSON(response.CommonResponse{
|
||||
Status: false,
|
||||
Message: err.Error(),
|
||||
})
|
||||
}
|
||||
|
||||
return c.Status(fiber.StatusOK).JSON(response.CommonResponse{
|
||||
Status: true,
|
||||
Data: res,
|
||||
})
|
||||
}
|
||||
|
||||
// RefreshToken godoc
|
||||
// @Summary Refresh access token
|
||||
// @Description Get a new access token using the user's current session/refresh token
|
||||
// @Tags Auth
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Security BearerAuth
|
||||
// @Success 200 {object} response.CommonResponse
|
||||
// @Failure 500 {object} response.CommonResponse
|
||||
// @Router /auth/refresh [post]
|
||||
func (h *AuthController) RefreshToken(c fiber.Ctx) error {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
defer cancel()
|
||||
|
||||
res, err := h.service.RefreshToken(ctx, c.Locals("uid").(string))
|
||||
if err != nil {
|
||||
return c.Status(fiber.StatusInternalServerError).JSON(response.CommonResponse{
|
||||
Status: false,
|
||||
Message: err.Error(),
|
||||
})
|
||||
}
|
||||
|
||||
return c.Status(fiber.StatusOK).JSON(response.CommonResponse{
|
||||
Status: true,
|
||||
Data: res,
|
||||
})
|
||||
}
|
||||
|
||||
113
internal/controllers/tileController.go
Normal file
113
internal/controllers/tileController.go
Normal file
@@ -0,0 +1,113 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"history-api/internal/dtos/response"
|
||||
"history-api/internal/services"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/gofiber/fiber/v3"
|
||||
)
|
||||
|
||||
type TileController struct {
|
||||
service services.TileService
|
||||
}
|
||||
|
||||
func NewTileController(svc services.TileService) *TileController {
|
||||
return &TileController{service: svc}
|
||||
}
|
||||
|
||||
// GetMetadata godoc
|
||||
// @Summary Get tile metadata
|
||||
// @Description Retrieve map metadata
|
||||
// @Tags Tile
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Success 200 {object} response.CommonResponse
|
||||
// @Failure 500 {object} response.CommonResponse
|
||||
// @Router /tiles/metadata [get]
|
||||
func (h *TileController) GetMetadata(c fiber.Ctx) error {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
defer cancel()
|
||||
|
||||
res, err := h.service.GetMetadata(ctx)
|
||||
if err != nil {
|
||||
return c.Status(fiber.StatusInternalServerError).JSON(response.CommonResponse{
|
||||
Status: false,
|
||||
Message: err.Error(),
|
||||
})
|
||||
}
|
||||
|
||||
return c.Status(fiber.StatusOK).JSON(response.CommonResponse{
|
||||
Status: true,
|
||||
Data: res,
|
||||
})
|
||||
}
|
||||
|
||||
// GetTile godoc
|
||||
// @Summary Get a map tile
|
||||
// @Description Fetch vector or raster map tile data by Z, X, Y coordinates
|
||||
// @Tags Tile
|
||||
// @Produce application/octet-stream
|
||||
// @Param z path int true "Zoom level (0-22)"
|
||||
// @Param x path int true "X coordinate"
|
||||
// @Param y path int true "Y coordinate"
|
||||
// @Success 200 {file} byte
|
||||
// @Failure 400 {object} response.CommonResponse
|
||||
// @Failure 500 {object} response.CommonResponse
|
||||
// @Router /tiles/{z}/{x}/{y} [get]
|
||||
func (h *TileController) GetTile(c fiber.Ctx) error {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
defer cancel()
|
||||
|
||||
z, x, y, err := h.parseTileParams(c)
|
||||
if err != nil {
|
||||
return c.Status(fiber.StatusBadRequest).JSON(response.CommonResponse{
|
||||
Status: false,
|
||||
Message: err.Error(),
|
||||
})
|
||||
}
|
||||
|
||||
data, headers, err := h.service.GetTile(ctx, z, x, y)
|
||||
if err != nil {
|
||||
return c.Status(fiber.StatusInternalServerError).JSON(response.CommonResponse{
|
||||
Status: false,
|
||||
Message: err.Error(),
|
||||
})
|
||||
}
|
||||
|
||||
for k, v := range headers {
|
||||
c.Set(k, v)
|
||||
}
|
||||
|
||||
return c.Status(fiber.StatusOK).Send(data)
|
||||
}
|
||||
|
||||
func (h *TileController) parseTileParams(c fiber.Ctx) (int, int, int, error) {
|
||||
z, err := strconv.Atoi(c.Params("z"))
|
||||
if err != nil {
|
||||
return 0, 0, 0, fmt.Errorf("invalid z")
|
||||
}
|
||||
|
||||
x, err := strconv.Atoi(c.Params("x"))
|
||||
if err != nil {
|
||||
return 0, 0, 0, fmt.Errorf("invalid x")
|
||||
}
|
||||
|
||||
y, err := strconv.Atoi(c.Params("y"))
|
||||
if err != nil {
|
||||
return 0, 0, 0, fmt.Errorf("invalid y")
|
||||
}
|
||||
|
||||
if z < 0 || x < 0 || y < 0 {
|
||||
return 0, 0, 0, fmt.Errorf("coordinates must be positive")
|
||||
}
|
||||
|
||||
if z > 22 {
|
||||
return 0, 0, 0, fmt.Errorf("zoom level too large")
|
||||
}
|
||||
|
||||
return z, x, y, nil
|
||||
}
|
||||
Reference in New Issue
Block a user