-
Notifications
You must be signed in to change notification settings - Fork 0
/
getcitybyId.go
43 lines (36 loc) · 1.08 KB
/
getcitybyId.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package services
import (
"errors"
"net/http"
"github.com/Wolechacho/ticketmaster-backend/database/entities"
"gorm.io/gorm"
)
type GetCityByIdRequest struct {
Id string `param:"id"`
}
type CityModelResponse struct {
Id string `json:"id"`
Name string `json:"name"`
State string `json:"state"`
IsDeprecated bool `json:"isDeprecated"`
}
type GetCityByIdResponse struct {
City CityModelResponse
StatusCode int
}
func (cityService CityService) GetCityById(request GetCityByIdRequest) (GetCityByIdResponse, error) {
var err error
city := entities.City{}
result := cityService.DB.Where("Id = ? AND IsDeprecated = ?", request.Id, false).First(&city)
if result.Error != nil && errors.Is(result.Error, gorm.ErrRecordNotFound) {
err = errors.New("city record not found")
return GetCityByIdResponse{StatusCode: http.StatusNotFound}, err
}
cityResp := CityModelResponse{
Id: city.Id,
Name: city.Name,
State: city.State,
IsDeprecated: false,
}
return GetCityByIdResponse{City: cityResp, StatusCode: http.StatusOK}, nil
}