-
Notifications
You must be signed in to change notification settings - Fork 0
/
getmoviebyId.go
40 lines (32 loc) · 1.08 KB
/
getmoviebyId.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
package services
import (
"errors"
"net/http"
"github.com/Wolechacho/ticketmaster-backend/database/entities"
"gorm.io/gorm"
)
type GetMovieByIdRequest struct {
Id string `param:"id" validate:"required"`
}
type GetMovieByIdResponse struct {
StatusCode int
Movie MovieDataResponse `json:"movie"`
}
func (movieService MovieService) GetMovieById(request GetMovieByIdRequest) (GetMovieByIdResponse, error) {
movie := &entities.Movie{}
result := movieService.DB.Where("Id = ? AND IsDeprecated = ?", request.Id, false).First(&movie)
if result.Error != nil && errors.Is(result.Error, gorm.ErrRecordNotFound) {
return GetMovieByIdResponse{StatusCode: http.StatusNotFound}, errors.New("movie Record not found")
}
movieDataResp := MovieDataResponse{
Id: movie.Id,
Title: movie.Title,
Language: movie.Language,
Description: movie.Description.String,
ReleaseDate: movie.ReleaseDate,
Genre: movie.Genre,
Popularity: movie.Popularity,
VoteCount: movie.VoteCount,
}
return GetMovieByIdResponse{StatusCode: http.StatusOK, Movie: movieDataResp}, nil
}