Initial commit: dating app backend (Go/Gin/GORM/Postgres)
Phone+OTP auth, staged onboarding, admin approval workflow, and a full discovery/swipe/match/chat system with photo upload and approximate distance (haversine) based on user location. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,140 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"io"
|
||||
"net/http"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"gorm.io/gorm"
|
||||
|
||||
"dating-app/internal/dto"
|
||||
"dating-app/internal/repository"
|
||||
"dating-app/internal/service"
|
||||
"dating-app/internal/utils"
|
||||
)
|
||||
|
||||
type ProfileHandler struct {
|
||||
optionRepo *repository.OptionRepository
|
||||
questionRepo *repository.QuestionRepository
|
||||
photoRepo *repository.PhotoRepository
|
||||
onboardingService *service.OnboardingService
|
||||
profileService *service.ProfileService
|
||||
}
|
||||
|
||||
func NewProfileHandler(
|
||||
optionRepo *repository.OptionRepository,
|
||||
questionRepo *repository.QuestionRepository,
|
||||
photoRepo *repository.PhotoRepository,
|
||||
onboardingService *service.OnboardingService,
|
||||
profileService *service.ProfileService,
|
||||
) *ProfileHandler {
|
||||
return &ProfileHandler{
|
||||
optionRepo: optionRepo,
|
||||
questionRepo: questionRepo,
|
||||
photoRepo: photoRepo,
|
||||
onboardingService: onboardingService,
|
||||
profileService: profileService,
|
||||
}
|
||||
}
|
||||
|
||||
// Me GET /profile/me — پروفایل کامل کاربر شامل تمام انتخابها و پاسخها
|
||||
func (h *ProfileHandler) Me(c *gin.Context) {
|
||||
user, ok := currentUser(c)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
|
||||
payload := fullProfilePayload(user, h.optionRepo, h.questionRepo, h.onboardingService, h.photoRepo)
|
||||
utils.Success(c, http.StatusOK, "اطلاعات پروفایل با موفقیت دریافت شد", payload)
|
||||
}
|
||||
|
||||
// UploadPhoto POST /profile/photos (multipart/form-data, فیلد "photo")
|
||||
func (h *ProfileHandler) UploadPhoto(c *gin.Context) {
|
||||
user, ok := currentUser(c)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
|
||||
fileHeader, err := c.FormFile("photo")
|
||||
if err != nil {
|
||||
utils.Fail(c, http.StatusBadRequest, "فایل عکس ارسال نشده", nil)
|
||||
return
|
||||
}
|
||||
|
||||
ext := strings.ToLower(filepath.Ext(fileHeader.Filename))
|
||||
|
||||
file, err := fileHeader.Open()
|
||||
if err != nil {
|
||||
utils.Fail(c, http.StatusBadRequest, "خطا در خواندن فایل", nil)
|
||||
return
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
data, err := io.ReadAll(file)
|
||||
if err != nil {
|
||||
utils.Fail(c, http.StatusBadRequest, "خطا در خواندن فایل", nil)
|
||||
return
|
||||
}
|
||||
|
||||
photo, err := h.profileService.UploadPhoto(user.ID, ext, data)
|
||||
if err != nil {
|
||||
h.handlePhotoError(c, err)
|
||||
return
|
||||
}
|
||||
utils.Success(c, http.StatusCreated, "عکس اضافه شد", photo)
|
||||
}
|
||||
|
||||
// DeletePhoto DELETE /profile/photos/:id
|
||||
func (h *ProfileHandler) DeletePhoto(c *gin.Context) {
|
||||
user, ok := currentUser(c)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
id, err := strconv.ParseUint(c.Param("id"), 10, 64)
|
||||
if err != nil {
|
||||
utils.Fail(c, http.StatusBadRequest, "شناسه نامعتبر است", nil)
|
||||
return
|
||||
}
|
||||
if err := h.profileService.DeletePhoto(user.ID, uint(id)); err != nil {
|
||||
h.handlePhotoError(c, err)
|
||||
return
|
||||
}
|
||||
utils.Success(c, http.StatusOK, "عکس حذف شد", nil)
|
||||
}
|
||||
|
||||
func (h *ProfileHandler) handlePhotoError(c *gin.Context, err error) {
|
||||
switch {
|
||||
case errors.Is(err, gorm.ErrRecordNotFound):
|
||||
utils.Fail(c, http.StatusNotFound, "عکس یافت نشد", nil)
|
||||
case errors.Is(err, service.ErrPhotoNotOwned):
|
||||
utils.Fail(c, http.StatusForbidden, err.Error(), nil)
|
||||
case errors.Is(err, service.ErrTooManyPhotos),
|
||||
errors.Is(err, service.ErrInvalidImage),
|
||||
errors.Is(err, service.ErrImageTooLarge):
|
||||
utils.Fail(c, http.StatusBadRequest, err.Error(), nil)
|
||||
default:
|
||||
utils.Fail(c, http.StatusInternalServerError, "خطا در ذخیرهی عکس", nil)
|
||||
}
|
||||
}
|
||||
|
||||
// UpdateLocation PATCH /profile/location
|
||||
func (h *ProfileHandler) UpdateLocation(c *gin.Context) {
|
||||
user, ok := currentUser(c)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
var req dto.UpdateLocationRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
utils.Fail(c, http.StatusBadRequest, "ورودی نامعتبر است", err.Error())
|
||||
return
|
||||
}
|
||||
if err := h.profileService.UpdateLocation(user, req.Latitude, req.Longitude); err != nil {
|
||||
utils.Fail(c, http.StatusInternalServerError, "خطا در ذخیرهی موقعیت مکانی", nil)
|
||||
return
|
||||
}
|
||||
utils.Success(c, http.StatusOK, "موقعیت مکانی ثبت شد", nil)
|
||||
}
|
||||
Reference in New Issue
Block a user