diff --git a/build.gradle b/build.gradle index 3d7f760..a3a0649 100644 --- a/build.gradle +++ b/build.gradle @@ -21,7 +21,7 @@ dependencies { implementation 'org.springframework.boot:spring-boot-starter-data-jpa' implementation 'org.springframework.boot:spring-boot-starter-web' developmentOnly 'org.springframework.boot:spring-boot-devtools' - runtimeOnly 'org.postgresql:postgresql' + implementation 'org.postgresql:postgresql:42.7.4' testImplementation 'org.springframework.boot:spring-boot-starter-test' testRuntimeOnly 'org.junit.platform:junit-platform-launcher' } diff --git a/src/main/java/com/booleanuk/api/controller/GameController.java b/src/main/java/com/booleanuk/api/controller/GameController.java new file mode 100644 index 0000000..8d21faf --- /dev/null +++ b/src/main/java/com/booleanuk/api/controller/GameController.java @@ -0,0 +1,61 @@ +package com.booleanuk.api.controller; + +import com.booleanuk.api.model.Game; +import com.booleanuk.api.repository.GameRepository; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; +import org.springframework.web.server.ResponseStatusException; + +import java.util.List; + +@RestController +@RequestMapping("games") +public class GameController { + + @Autowired + private final GameRepository repository; + + public GameController(GameRepository repository) { + this.repository = repository; + } + + @GetMapping + public List getAll() { + return this.repository.findAll(); + } + + @GetMapping("{id}") + public ResponseEntity getById(@PathVariable("id") Integer id){ + Game game = null; + game = this.repository.findById(id).orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "Game not found with that ID.")); + return ResponseEntity.ok(game); + } + + @ResponseStatus(HttpStatus.CREATED) + @PostMapping + public ResponseEntity create(@RequestBody Game game) { + return new ResponseEntity(this.repository.save(game), HttpStatus.CREATED); + } + + @DeleteMapping("{id}") + public ResponseEntity delete(@PathVariable("id") Integer id) { + Game deleteGame = this.repository.findById(id).orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, " Game not found with that ID")); + this.repository.delete(deleteGame); + return ResponseEntity.ok(deleteGame); + } + + @PutMapping("{id}") + public ResponseEntity update(@PathVariable("id") Integer id, @RequestBody Game updated) { + Game gameToUpdate = getById(id).getBody(); + + gameToUpdate.setTitle(updated.getTitle()); + gameToUpdate.setGenre(updated.getGenre()); + gameToUpdate.setPublisher(updated.getPublisher()); + gameToUpdate.setDeveloper(updated.getDeveloper()); + gameToUpdate.setReleaseYear(updated.getReleaseYear()); + gameToUpdate.setEarlyAccess(updated.isEarlyAccess()); + return new ResponseEntity(this.repository.save(gameToUpdate), HttpStatus.CREATED); + } +} diff --git a/src/main/java/com/booleanuk/api/controller/UserController.java b/src/main/java/com/booleanuk/api/controller/UserController.java index 5880187..d15dfa5 100644 --- a/src/main/java/com/booleanuk/api/controller/UserController.java +++ b/src/main/java/com/booleanuk/api/controller/UserController.java @@ -1,15 +1,21 @@ package com.booleanuk.api.controller; +import com.booleanuk.api.model.Game; import com.booleanuk.api.model.User; import com.booleanuk.api.repository.UserRepository; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; +import org.springframework.web.server.ResponseStatusException; import java.util.List; @RestController @RequestMapping("users") public class UserController { + + @Autowired private final UserRepository repository; public UserController(UserRepository repository) { @@ -22,16 +28,34 @@ public List getAll() { } @GetMapping("{id}") - public User getById(@PathVariable("id") Integer id) { - return this.repository.findById(id).orElseThrow(); + public ResponseEntity getById(@PathVariable("id") Integer id) { + User user = null; + user = this.repository.findById(id).orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "User not found with that ID.")); + return ResponseEntity.ok(user); } - record PostUser(String email, String firstName) {} - @ResponseStatus(HttpStatus.CREATED) @PostMapping - public User create(@RequestBody PostUser request) { - User user = new User(request.email(), request.firstName()); - return this.repository.save(user); + public ResponseEntity create(@RequestBody User user) { + return new ResponseEntity(this.repository.save(user), HttpStatus.CREATED); + } + + @DeleteMapping("{id}") + public ResponseEntity delete(@PathVariable("id") Integer id) { + User deleteUser = this.repository.findById(id).orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "User not found with that ID")); + this.repository.delete(deleteUser); + return ResponseEntity.ok(deleteUser); + } + + @PutMapping("{id}") + public ResponseEntity update(@PathVariable("id") Integer id, @RequestBody User updated) { + User userToUpdate = getById(id).getBody(); + + userToUpdate.setEmail(updated.getEmail()); + userToUpdate.setFirstName(updated.getFirstName()); + userToUpdate.setLastName(updated.getLastName()); + userToUpdate.setUsername(updated.getUsername()); + userToUpdate.setPhone(updated.getPhone()); + return new ResponseEntity(this.repository.save(userToUpdate), HttpStatus.CREATED); } } diff --git a/src/main/java/com/booleanuk/api/model/Game.java b/src/main/java/com/booleanuk/api/model/Game.java new file mode 100644 index 0000000..d6761b1 --- /dev/null +++ b/src/main/java/com/booleanuk/api/model/Game.java @@ -0,0 +1,109 @@ +package com.booleanuk.api.model; + +import jakarta.persistence.*; + +@Entity +@Table(name = "Games") +public class Game { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Integer id; + + private String title; + private String genre; + private String publisher; + private String developer; + private int releaseYear; + private boolean isEarlyAccess; + + public Game(Integer id, String title, String genre, String publisher, String developer, int releaseYear, boolean isEarlyAccess) { + this.id = id; + this.title = title; + this.genre = genre; + this.publisher = publisher; + this.developer = developer; + this.releaseYear = releaseYear; + this.isEarlyAccess = isEarlyAccess; + } + + public Game(String title, String genre, String publisher, String developer, int releaseYear, boolean isEarlyAccess) { + this.title = title; + this.genre = genre; + this.publisher = publisher; + this.developer = developer; + this.releaseYear = releaseYear; + this.isEarlyAccess = isEarlyAccess; + } + + public Game() {} + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + public String getGenre() { + return genre; + } + + public void setGenre(String genre) { + this.genre = genre; + } + + public String getPublisher() { + return publisher; + } + + public void setPublisher(String publisher) { + this.publisher = publisher; + } + + public String getDeveloper() { + return developer; + } + + public void setDeveloper(String developer) { + this.developer = developer; + } + + public int getReleaseYear() { + return releaseYear; + } + + public void setReleaseYear(int releaseYear) { + this.releaseYear = releaseYear; + } + + public boolean isEarlyAccess() { + return isEarlyAccess; + } + + public void setEarlyAccess(boolean earlyAccess) { + isEarlyAccess = earlyAccess; + } + + + @Override + public String toString() { + return "Game{" + + "id=" + id + + ", title='" + title + '\'' + + ", genre='" + genre + '\'' + + ", publisher='" + publisher + '\'' + + ", developer='" + developer + '\''+ + ", releaseYear=" + releaseYear + + ", isEarylAccess=" + isEarlyAccess + + '}'; + } +} diff --git a/src/main/java/com/booleanuk/api/model/User.java b/src/main/java/com/booleanuk/api/model/User.java index 8b7a537..e5e95c7 100644 --- a/src/main/java/com/booleanuk/api/model/User.java +++ b/src/main/java/com/booleanuk/api/model/User.java @@ -1,13 +1,11 @@ package com.booleanuk.api.model; -import jakarta.persistence.Entity; -import jakarta.persistence.GeneratedValue; -import jakarta.persistence.GenerationType; -import jakarta.persistence.Id; +import jakarta.persistence.*; import java.util.Objects; @Entity +@Table(name = "Users") public class User { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @@ -15,21 +13,29 @@ public class User { private String email; private String firstName; - private Boolean isActive; + private String lastName; + private String username; + private String phone; - public User(Integer id, String email, String firstName, Boolean isActive) { + public User(Integer id, String email, String firstName, String lastName, String username, String phone) { this.id = id; this.email = email; this.firstName = firstName; - this.isActive = isActive; + this.lastName = lastName; + this.username = username; + this.phone = phone; } - public User(String email, String firstName) { + public User(String email, String firstName, String lastName, String username, String phone) { this.email = email; this.firstName = firstName; - this.isActive = false; + this.lastName = lastName; + this.username = username; + this.phone = phone; } + public User() {} + public Integer getId() { return id; } @@ -54,12 +60,28 @@ public void setFirstName(String firstName) { this.firstName = firstName; } - public Boolean getActive() { - return isActive; + public String getPhone() { + return phone; + } + + public void setPhone(String phone) { + this.phone = phone; + } + + public String getUsername() { + return username; + } + + public void setUsername(String username) { + this.username = username; + } + + public String getLastName() { + return lastName; } - public void setActive(Boolean active) { - isActive = active; + public void setLastName(String lastName) { + this.lastName = lastName; } @Override @@ -67,12 +89,12 @@ public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; User user = (User) o; - return Objects.equals(id, user.id) && Objects.equals(email, user.email) && Objects.equals(firstName, user.firstName) && Objects.equals(isActive, user.isActive); + return Objects.equals(id, user.id) && Objects.equals(email, user.email) && Objects.equals(firstName, user.firstName) && Objects.equals(lastName, user.lastName) && Objects.equals(username, user.username) && Objects.equals(phone, user.phone); } @Override public int hashCode() { - return Objects.hash(id, email, firstName, isActive); + return Objects.hash(id, email, firstName, lastName, username, phone); } @Override @@ -81,7 +103,9 @@ public String toString() { "id=" + id + ", email='" + email + '\'' + ", firstName='" + firstName + '\'' + - ", isActive=" + isActive + + ", lastName='" + lastName + '\'' + + ", username='" + username + '\'' + + ", phone='" + phone + '\'' + '}'; } } diff --git a/src/main/java/com/booleanuk/api/repository/GameRepository.java b/src/main/java/com/booleanuk/api/repository/GameRepository.java new file mode 100644 index 0000000..d2acfe4 --- /dev/null +++ b/src/main/java/com/booleanuk/api/repository/GameRepository.java @@ -0,0 +1,8 @@ +package com.booleanuk.api.repository; + +import com.booleanuk.api.model.Game; +import org.springframework.data.jpa.repository.JpaRepository; + +public interface GameRepository extends JpaRepository { + +} diff --git a/src/main/resources/application.yml.example b/src/main/resources/application.yml.example deleted file mode 100644 index 03bcb31..0000000 --- a/src/main/resources/application.yml.example +++ /dev/null @@ -1,24 +0,0 @@ -server: - port: 4000 - - error: - include-message: always - include-binding-errors: always - include-stacktrace: never - include-exception: false - -spring: - datasource: - url: jdbc:postgresql://DATABASE_URL - username: DATABASE_USERNAME - password: DATABASE_PASSWORD - max-active: 3 - max-idle: 3 - jpa: - hibernate: - ddl-auto: create-drop - properties: - hibernate: - dialect: org.hibernate.dialect.PostgreSQLDialect - format_sql: true - show-sql: true \ No newline at end of file