Skip to content

Commit

Permalink
feat: GetMovieDetials [User Story 4] & make use of DTOS
Browse files Browse the repository at this point in the history
  • Loading branch information
anqorithm committed Sep 1, 2023
1 parent 1932c07 commit 76f68c1
Show file tree
Hide file tree
Showing 9 changed files with 538 additions and 177 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ This is a service for gitting the latest movies from IMDB API, feed the database

## Important commands to compile the protocol buffers

You have to compile the protocol buffers every time you change the pf :)

```sh

$ export PATH="$PATH:$(go env GOPATH)/bin"
$ protoc --go_out=. --go_opt=paths=source_relative --go-grpc_out=. --go-grpc_opt=paths=source_relative proto/movies.proto
$ protoc --go_out=. --go_opt=paths=source_relative --go-grpc_out=. --go-grpc_opt=paths=source_relative grpc/proto/movies.proto

```

Expand All @@ -33,5 +35,6 @@ ACCESS_TOKEN_AUTH=
## How to run?

```
$ sudo chmod -R 777 postgres_data
$ docker-compose up
```
2 changes: 1 addition & 1 deletion docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ COPY go.mod go.sum ./

RUN go mod download

COPY . .
COPY . .

RUN CGO_ENABLED=0 GOOS=linux go build -o /movies

Expand Down
7 changes: 7 additions & 0 deletions dtos/dtos.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,10 @@ type MovieDTO struct {
VoteAverage float32 `json:"vote_average"`
VoteCount int32 `json:"vote_count"`
}

type MoveDetailsDTO struct {
ID int32 `json:"id"`
Title string `json:"name"`
PosterPath string `json:"poster"`
Overview string `json:"description"`
}
30 changes: 26 additions & 4 deletions grpc/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ func getLatestMovies(client proto.MoviesServiceClient) {
}

func searchMovies(client proto.MoviesServiceClient, query string) {
response, err := client.SearchMovies(context.Background(), &proto.SearchMoviesRequest{Query: query})
res, err := client.SearchMovies(context.Background(), &proto.SearchMoviesRequest{Query: query})
if err != nil {
log.Fatalf("err: %v", err)
}
for _, movie := range response.Movies {
for _, movie := range res.Movies {
movieDTO := helpers.MovieProtoToDTO(movie)
movieJSON, err := json.Marshal(movieDTO)
if err != nil {
Expand All @@ -59,8 +59,24 @@ func updateFavourites(client proto.MoviesServiceClient, action proto.FavouriteAc
log.Printf("%v", res.Message)
}

func getMovieDetials(client proto.MoviesServiceClient, movie_id uint32) {
res, err := client.GetMovieDetials(context.Background(), &proto.MovieDetialsRequest{MovieId: movie_id})
if err != nil {
log.Fatalf("err: %v", err)
}
movieDTO := helpers.MoveDetailsProtoToDTO(res.MovieDetails)
movieJSON, err := json.Marshal(movieDTO)
if err != nil {
log.Fatalf("err: %v", err)
}
var out bytes.Buffer
json.Indent(&out, movieJSON, "", " ")
out.WriteTo(os.Stdout)
println(",")
}

func main() {
connection, err := grpc.Dial("localhost:50051", grpc.WithTransportCredentials(insecure.NewCredentials()))
connection, err := grpc.Dial("127.0.0.1:50051", grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
log.Fatalf("err: %v", err)
}
Expand All @@ -70,7 +86,8 @@ func main() {
fmt.Println("1: Get latest movies")
fmt.Println("2: Search movies")
fmt.Println("3: Update Favorites")
fmt.Print("Enter a number (1-3): ")
fmt.Println("4: Get movie details")
fmt.Print("Enter a number (1-4): ")
fmt.Scanln(&choice)
switch strings.TrimSpace(choice) {
case "1":
Expand All @@ -91,6 +108,11 @@ func main() {
fmt.Print("Enter Action: ")
fmt.Scanln(&action)
updateFavourites(client, action, userId, movieId)
case "4":
var movieId uint32
fmt.Print("Enter MovieID: ")
fmt.Scanln(&movieId)
getMovieDetials(client, movieId)
default:
log.Fatalf("Invalid choice: %s", choice)
}
Expand Down
Loading

0 comments on commit 76f68c1

Please sign in to comment.