-
Notifications
You must be signed in to change notification settings - Fork 0
/
getmovies.go
88 lines (73 loc) · 2.44 KB
/
getmovies.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package services
import (
"fmt"
"time"
"github.com/Wolechacho/ticketmaster-backend/database/entities"
paginate "github.com/Wolechacho/ticketmaster-backend/helpers/pagination"
)
type GetMoviesRequest struct {
Page int `query:"page"`
PageLength int `query:"pageLength"`
SortBy string `query:"sortBy"`
Order string `query:"order"`
}
type GetMoviesResponse struct {
Page int `json:"page"`
PerPage int `json:"perPage"`
TotalResults int64 `json:"totalResults"`
RequestedPageLength int `json:"requestedPageLength"`
Movies []MovieDataResponse `json:"movies"`
}
type MovieDataResponse struct {
Id string `json:"id"`
Title string `json:"title"`
Description string `json:"description"`
Language string `json:"language"`
ReleaseDate time.Time `json:"releaseDate"`
Genre int `json:"genre"`
Popularity float32 `json:"popularity"`
VoteCount int `json:"voteCount"`
}
func (movieService MovieService) GetMovies(request GetMoviesRequest) (GetMoviesResponse, error) {
if request.Page <= 0 {
request.Page = 1
}
switch {
case request.PageLength > 100:
request.PageLength = 100
case request.PageLength <= 0:
request.PageLength = 10
}
//Filter
filterClause := paginate.FilterFields(&entities.Movie{IsDeprecated: false})
//paginate
movies := new([]entities.Movie)
paginateClause := paginate.Paginate(request.Page, request.PageLength)
//order by
sortandorder := fmt.Sprintf("%s %s", request.SortBy, request.Order)
fmt.Println(sortandorder)
orderByClause := paginate.OrderBy(sortandorder)
//this uses functional scope pattern in golang
movieService.DB.Scopes(filterClause, orderByClause, paginateClause).Find(&movies)
var countResult int64
paginate.GetEntityCount(movieService.DB, new(entities.Movie), &countResult)
resp := new(GetMoviesResponse)
resp.Page = request.Page
resp.RequestedPageLength = request.PageLength
resp.PerPage = len(*movies)
resp.TotalResults = countResult
for _, movie := range *movies {
movieData := 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,
}
resp.Movies = append(resp.Movies, movieData)
}
return *resp, nil
}