/
Arsinenko
/
CompClubAPI_Go
Обзор
Документация
Войти
/
Arsinenko
/
CompClubAPI_Go
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
main
internal/services/working_space_service.go
128 строк
3 KB
Arsinenko
Working space update
17 ноя 2025, 17:41
17 ноя 2025, 17:41
d647065
Код
Авторство
О чём код?
package services import ( "CompClubAPI/internal/database/gen" "context" "database/sql" "errors" ) type WorkingSpaceResponse struct { Id int32 `json:"id"` Enabled bool `json:"enabled"` Club string `json:"club"` Tariff string `json:"tariff"` } type CreateWorkingSpaceRequest struct { Club string `json:"club"` Tariff string `json:"tariff"` } type WorkingSpaceService interface { CreateWorkingSpace(req CreateWorkingSpaceRequest) (WorkingSpaceResponse, error) UpdateWorkingSpaceTariff(id int32, tariff string) (WorkingSpaceResponse, error) UpdateStatus(id int32, status bool) (WorkingSpaceResponse, error) GetWorkingSpace(id int32) (WorkingSpaceResponse, error) GetWorkingSpaces() ([]WorkingSpaceResponse, error) } type workingSpaceService struct { queries *gen.Queries } func NewWorkingSpaceService(queries *gen.Queries) WorkingSpaceService { return workingSpaceService{queries: queries} } func (w workingSpaceService) CreateWorkingSpace(req CreateWorkingSpaceRequest) (WorkingSpaceResponse, error) { ws, err := w.queries.CreateWorkingSpace(context.Background(), gen.CreateWorkingSpaceParams{ Club: req.Club, Tariff: req.Tariff, }) if err != nil { return WorkingSpaceResponse{}, err } return WorkingSpaceResponse{ Id: ws.ID, Enabled: ws.Enabled.Bool, Club: ws.Club, Tariff: ws.Tariff, }, nil } func (w workingSpaceService) UpdateWorkingSpaceTariff(id int32, tariff string) (WorkingSpaceResponse, error) { ws, err := w.queries.UpdateWorkingSpaceTariff(context.Background(), gen.UpdateWorkingSpaceTariffParams{ ID: id, Tariff: tariff, }) if err != nil { if errors.Is(err, sql.ErrNoRows) { return WorkingSpaceResponse{}, errors.New("working space not found") } return WorkingSpaceResponse{}, err } return WorkingSpaceResponse{ Id: ws.ID, Enabled: ws.Enabled.Bool, Club: ws.Club, Tariff: ws.Tariff, }, nil } func (w workingSpaceService) UpdateStatus(id int32, status bool) (WorkingSpaceResponse, error) { ws, err := w.queries.UpdateStatus(context.Background(), gen.UpdateStatusParams{ ID: id, Enabled: sql.NullBool{ Bool: status, Valid: true, }, }) if err != nil { if errors.Is(err, sql.ErrNoRows) { return WorkingSpaceResponse{}, errors.New("working space not found") } return WorkingSpaceResponse{}, err } return WorkingSpaceResponse{ Id: ws.ID, Enabled: ws.Enabled.Bool, Club: ws.Club, Tariff: ws.Tariff, }, nil } func (w workingSpaceService) GetWorkingSpace(id int32) (WorkingSpaceResponse, error) { ws, err := w.queries.GetWorkingSpace(context.Background(), id) if err != nil { if errors.Is(err, sql.ErrNoRows) { return WorkingSpaceResponse{}, errors.New("working space not found") } return WorkingSpaceResponse{}, err } return WorkingSpaceResponse{ Id: ws.ID, Enabled: ws.Enabled.Bool, Club: ws.Club, Tariff: ws.Tariff, }, nil } func (w workingSpaceService) GetWorkingSpaces() ([]WorkingSpaceResponse, error) { wss, err := w.queries.GetWorkingSpaces(context.Background()) if err != nil { if errors.Is(err, sql.ErrNoRows) { return nil, errors.New("working spaces not found") } return nil, err } var result []WorkingSpaceResponse for _, ws := range wss { result = append(result, WorkingSpaceResponse{ Id: ws.ID, Enabled: ws.Enabled.Bool, Club: ws.Club, Tariff: ws.Tariff, }) } return result, nil }