Skip to content

Commit

Permalink
Merge pull request #26 from gyda13/lulu
Browse files Browse the repository at this point in the history
register User endpoint, db fixes
  • Loading branch information
LulwahAlmisfer authored Feb 5, 2024
2 parents f24bd8f + 64c2d9f commit b4793d3
Show file tree
Hide file tree
Showing 19 changed files with 100 additions and 27 deletions.
Binary file modified dbDesign.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 1 addition & 2 deletions src/main/java/com/lulugyda/controllers/MovieController.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@


@Controller("v1/movies")
@Slf4j
@Validated
@RequiredArgsConstructor
public class MovieController {
Expand Down Expand Up @@ -67,7 +66,7 @@ public HttpResponse<MovieDetailsResponse> getMovieDetails(

@Get(value = "/favourites", produces = APPLICATION_JSON)
@ExecuteOn(TaskExecutors.IO)
public HttpResponse<?> saveUserMovies(@Header(USER_ID) String userId) {
public HttpResponse<?> saveUserMovies(@Header(USER_ID) Integer userId) {
movieService.saveUserMovies(userId);
return HttpResponse.ok();
}
Expand Down
42 changes: 42 additions & 0 deletions src/main/java/com/lulugyda/controllers/UsersController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.lulugyda.controllers;

import com.lulugyda.models.entities.PhoneNumberEntity;
import com.lulugyda.models.entities.UserEntity;
import com.lulugyda.services.MovieService;
import io.micronaut.http.HttpResponse;
import io.micronaut.http.annotation.Body;
import io.micronaut.http.annotation.Controller;
import io.micronaut.http.annotation.Header;
import io.micronaut.http.annotation.Post;
import io.micronaut.scheduling.TaskExecutors;
import io.micronaut.scheduling.annotation.ExecuteOn;
import io.micronaut.validation.Validated;
import lombok.RequiredArgsConstructor;

import java.util.ArrayList;

import static com.lulugyda.utils.Constants.USER_ID;
import static io.micronaut.http.MediaType.APPLICATION_JSON;

@Controller("v1/users")
@Validated
@RequiredArgsConstructor
public class UsersController {

private final MovieService movieService;
@Post(produces = APPLICATION_JSON)
@ExecuteOn(TaskExecutors.IO)
public HttpResponse<?> registerUser(@Body UserEntity user) {
movieService.registerUser(user);
return HttpResponse.ok();
}

// @Post(value = "/phone-numbers", produces = APPLICATION_JSON)
// @ExecuteOn(TaskExecutors.IO)
// public HttpResponse<?> addPhoneNumbers(@Body ArrayList<PhoneNumberEntity> numbers,
// @Header(USER_ID) Integer userId) {
// movieService.addPhoneNumbers(numbers,userId);
// return HttpResponse.ok();
// }

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class MovieEntity {
@Id
@Column(name = "movie_id", unique = true)
@JsonProperty("movieId")
private String movieId;
private Integer movieId;

@Column(name = "movie_title")
@JsonProperty("movieTitle")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.lulugyda.models.entities;

import io.micronaut.core.annotation.Introspected;
import io.micronaut.serde.annotation.SerdeImport;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
Expand All @@ -18,12 +19,13 @@
@AllArgsConstructor
@NoArgsConstructor
@Introspected
@SerdeImport(PhoneNumberEntity.class)
public class PhoneNumberEntity {

@Id
@GeneratedValue(strategy = GenerationType.UUID)
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", unique = true, nullable = false)
private String id;
private Integer id;

@Column(name = "mobile_number")
private String mobileNumber;
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/com/lulugyda/models/entities/UserEntity.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.lulugyda.models.entities;

import com.fasterxml.jackson.annotation.JsonIgnore;
import io.micronaut.core.annotation.Introspected;
import io.micronaut.serde.annotation.Serdeable;
import jakarta.persistence.*;
Expand All @@ -26,9 +27,9 @@
public class UserEntity {

@Id
@GeneratedValue(strategy = GenerationType.UUID)
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", unique = true, nullable = false)
private String id;
private Integer id;

@Column(name = "username")
private String username;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ public class UserMoviesEntity {

@Id
@Column(name = "user_id")
private String userId;
private Integer userId;

@Column(name = "movie_id")
private String movieId;
private Integer movieId;

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

import com.lulugyda.models.entities.MovieEntity;
import io.micronaut.data.annotation.Repository;
import io.micronaut.data.jdbc.annotation.JdbcRepository;
import io.micronaut.data.model.query.builder.sql.Dialect;
import io.micronaut.data.repository.CrudRepository;

@Repository
public interface MoviesCrudRepository extends CrudRepository<MovieEntity, String> {
public interface MoviesCrudRepository extends CrudRepository<MovieEntity, Integer> {
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ public class MoviesCrudRepositoryFacade {

private final UsersCrudRepositoryFacade usersCrudRepositoryFacade;

public void saveUserMovies(String userId) {
public void saveUserMovies(Integer userId) {
try {
log.info("saveUserMovies for user id {}", userId);

MovieEntity m1 = new MovieEntity("1", "t1", null);
MovieEntity m2 = new MovieEntity("2", "gydaamovie", null);
MovieEntity m1 = new MovieEntity(1, "t1", null);
MovieEntity m2 = new MovieEntity(2, "gydaamovie", null);
List<MovieEntity> movies = Arrays.asList(m1, m2);

List<MovieEntity> saveMovies = movies.stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
import io.micronaut.data.repository.CrudRepository;

@Repository
public interface PhoneNumbersCrudRepository extends CrudRepository<PhoneNumberEntity, String> {
public interface PhoneNumbersCrudRepository extends CrudRepository<PhoneNumberEntity, Integer> {
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
import io.micronaut.data.repository.CrudRepository;

@Repository
public interface UsersCrudRepository extends CrudRepository<UserEntity, String> {
public interface UsersCrudRepository extends CrudRepository<UserEntity, Integer> {
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public UserEntity saveUser(UserEntity userEntity) {
}
}

public Optional<UserEntity> findUser(String userId) {
public Optional<UserEntity> findUser(Integer userId) {
Optional<UserEntity> userEntity = Optional.empty();

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
import io.micronaut.data.repository.CrudRepository;

@Repository
public interface UsersMoviesCrudRepository extends CrudRepository<UserMoviesEntity, String> {
public interface UsersMoviesCrudRepository extends CrudRepository<UserMoviesEntity, Integer> {
}
10 changes: 9 additions & 1 deletion src/main/java/com/lulugyda/services/MovieService.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
package com.lulugyda.services;

import com.lulugyda.models.entities.PhoneNumberEntity;
import com.lulugyda.models.entities.UserEntity;
import com.lulugyda.models.responses.MovieDetailsResponse;
import com.lulugyda.models.responses.MovieListResponse;

import java.util.ArrayList;

public interface MovieService {
MovieListResponse getMovieList(String page);

MovieDetailsResponse getMovieDetails(String movieId);

void saveUserMovies(String userId);
void saveUserMovies(Integer userId);

void registerUser (UserEntity userEntity);

void addPhoneNumbers (ArrayList<PhoneNumberEntity> numbers, Integer userّId);

}
24 changes: 22 additions & 2 deletions src/main/java/com/lulugyda/services/MovieServiceFacade.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import com.lulugyda.clients.models.responses.TmdbMovieListResponse;
import com.lulugyda.clients.models.responses.TmdbMovieReviewersResponse;
import com.lulugyda.mappers.TmdbMovieDetailsMapper;
import com.lulugyda.models.entities.PhoneNumberEntity;
import com.lulugyda.models.entities.UserEntity;
import com.lulugyda.models.responses.MovieDetailsResponse;
import com.lulugyda.models.responses.MovieListResponse;
import com.lulugyda.repositories.PhoneNumbersCrudRepositoryFacade;
Expand All @@ -14,6 +16,9 @@
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.Optional;

@Slf4j
@Singleton
@RequiredArgsConstructor
Expand Down Expand Up @@ -51,10 +56,25 @@ public MovieDetailsResponse getMovieDetails(String movieId) {
}

@Override
public void saveUserMovies(String userId) {
public void saveUserMovies(Integer userId) {
moviesCrudRepositoryFacade.saveUserMovies(userId);
}

@Override
public void registerUser(UserEntity userEntity) {
usersCrudRepositoryFacade.saveUser(userEntity);
}


@Override
public void addPhoneNumbers(ArrayList<PhoneNumberEntity> numbers, Integer userId) {
Optional<UserEntity> user = usersCrudRepositoryFacade.findUser(userId);

if (user.isPresent()) {
for (PhoneNumberEntity number : numbers) {
number.setUser(user.get());
System.out.println(number.getMobileNumber());
phoneNumbersCrudRepositoryFacade.savePhoneNumber(number);
}
}
}
}
2 changes: 1 addition & 1 deletion src/main/resources/db/migration/V1__Create-Users-Table.sql
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
CREATE TABLE Users (
id varchar(255) PRIMARY KEY,
id serial PRIMARY KEY,
username varchar NOT NULL,
password varchar NOT NULL,
email varchar NOT NULL
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
CREATE TABLE Phone_Numbers (
id varchar(255) PRIMARY KEY,
user_id varchar(255) NOT NULL,
id serial PRIMARY KEY,
user_id INTEGER NOT NULL,
mobile_number varchar NOT NULL,
CONSTRAINT fk_users FOREIGN KEY (user_id) REFERENCES Users(id)
);
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
CREATE TABLE Movies (
movie_id varchar(255) PRIMARY KEY,
movie_id INTEGER PRIMARY KEY,
movie_title varchar(255) NOT NULL
);
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
CREATE TABLE Users_Movies (

user_id varchar(255) NOT NULL,
movie_id varchar(255) NOT NULL,
user_id INTEGER NOT NULL,
movie_id INTEGER NOT NULL,
FOREIGN KEY (user_id) REFERENCES Users (id),
FOREIGN KEY (movie_id) REFERENCES Movies (movie_id)
);

0 comments on commit b4793d3

Please sign in to comment.