90a96b4125
- Remove api_es service, Dockerfile, all Go source files - Remove api_es from docker-compose.yml, nginx-ssl.conf, .env, Makefile - Replace nginx /api/ proxy with /api/v1/ → api_yal:8787 - Add amenity/upload domains, AuthResponse, GET /auth/me, GET /objects/my to api_yal - Rewrite easysite frontend: types, composables, and all 5 pages to use api_yal DTOs - Wire nuxt.config public.apiBase, add useObjects CRUD composable - Update docs references from api_es to api_yal
126 lines
3.1 KiB
Go
126 lines
3.1 KiB
Go
package amenity
|
|
|
|
import (
|
|
"api_yal/internal/models"
|
|
"api_yal/internal/repository"
|
|
"errors"
|
|
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type AmenityService interface {
|
|
Create(req *CreateAmenityRequest) (*AmenityResponse, error)
|
|
GetByID(id uint) (*AmenityResponse, error)
|
|
Update(id uint, req *UpdateAmenityRequest) (*AmenityResponse, error)
|
|
Delete(id uint) error
|
|
List() ([]AmenityResponse, error)
|
|
ListByCategory(category string) ([]AmenityResponse, error)
|
|
GetByObject(objectID uint) ([]AmenityResponse, error)
|
|
ReplaceObjectAmenities(objectID uint, amenityIDs []uint) error
|
|
}
|
|
|
|
type amenityServiceImpl struct {
|
|
repo repository.AmenityRepository
|
|
}
|
|
|
|
func NewService(repo repository.AmenityRepository) AmenityService {
|
|
return &amenityServiceImpl{repo: repo}
|
|
}
|
|
|
|
func (s *amenityServiceImpl) Create(req *CreateAmenityRequest) (*AmenityResponse, error) {
|
|
amenity := &models.Amenity{
|
|
Name: req.Name,
|
|
Category: req.Category,
|
|
Icon: req.Icon,
|
|
Description: req.Description,
|
|
}
|
|
if err := s.repo.Create(amenity); err != nil {
|
|
return nil, err
|
|
}
|
|
resp := ToAmenityResponse(amenity)
|
|
return &resp, nil
|
|
}
|
|
|
|
func (s *amenityServiceImpl) GetByID(id uint) (*AmenityResponse, error) {
|
|
amenity, err := s.repo.GetByID(id)
|
|
if err != nil {
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
return nil, errors.New("amenity not found")
|
|
}
|
|
return nil, err
|
|
}
|
|
resp := ToAmenityResponse(amenity)
|
|
return &resp, nil
|
|
}
|
|
|
|
func (s *amenityServiceImpl) Update(id uint, req *UpdateAmenityRequest) (*AmenityResponse, error) {
|
|
amenity, err := s.repo.GetByID(id)
|
|
if err != nil {
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
return nil, errors.New("amenity not found")
|
|
}
|
|
return nil, err
|
|
}
|
|
if req.Name != nil {
|
|
amenity.Name = *req.Name
|
|
}
|
|
if req.Category != nil {
|
|
amenity.Category = *req.Category
|
|
}
|
|
if req.Icon != nil {
|
|
amenity.Icon = *req.Icon
|
|
}
|
|
if req.Description != nil {
|
|
amenity.Description = *req.Description
|
|
}
|
|
if err := s.repo.Update(amenity); err != nil {
|
|
return nil, err
|
|
}
|
|
resp := ToAmenityResponse(amenity)
|
|
return &resp, nil
|
|
}
|
|
|
|
func (s *amenityServiceImpl) Delete(id uint) error {
|
|
return s.repo.Delete(id)
|
|
}
|
|
|
|
func (s *amenityServiceImpl) List() ([]AmenityResponse, error) {
|
|
amenities, err := s.repo.List()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
resp := make([]AmenityResponse, len(amenities))
|
|
for i, a := range amenities {
|
|
resp[i] = ToAmenityResponse(&a)
|
|
}
|
|
return resp, nil
|
|
}
|
|
|
|
func (s *amenityServiceImpl) ListByCategory(category string) ([]AmenityResponse, error) {
|
|
amenities, err := s.repo.ListByCategory(category)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
resp := make([]AmenityResponse, len(amenities))
|
|
for i, a := range amenities {
|
|
resp[i] = ToAmenityResponse(&a)
|
|
}
|
|
return resp, nil
|
|
}
|
|
|
|
func (s *amenityServiceImpl) GetByObject(objectID uint) ([]AmenityResponse, error) {
|
|
amenities, err := s.repo.GetByObject(objectID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
resp := make([]AmenityResponse, len(amenities))
|
|
for i, a := range amenities {
|
|
resp[i] = ToAmenityResponse(&a)
|
|
}
|
|
return resp, nil
|
|
}
|
|
|
|
func (s *amenityServiceImpl) ReplaceObjectAmenities(objectID uint, amenityIDs []uint) error {
|
|
return s.repo.ReplaceObjectAmenities(objectID, amenityIDs)
|
|
}
|