Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Leo Wahlandt #63

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
Expand Down
61 changes: 61 additions & 0 deletions src/main/java/com/booleanuk/api/controller/GameController.java
Original file line number Diff line number Diff line change
@@ -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<Game> getAll() {
return this.repository.findAll();
}

@GetMapping("{id}")
public ResponseEntity<Game> 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<Game> create(@RequestBody Game game) {
return new ResponseEntity<Game>(this.repository.save(game), HttpStatus.CREATED);
}

@DeleteMapping("{id}")
public ResponseEntity<Game> 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<Game> 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<Game>(this.repository.save(gameToUpdate), HttpStatus.CREATED);
}
}
38 changes: 31 additions & 7 deletions src/main/java/com/booleanuk/api/controller/UserController.java
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -22,16 +28,34 @@ public List<User> getAll() {
}

@GetMapping("{id}")
public User getById(@PathVariable("id") Integer id) {
return this.repository.findById(id).orElseThrow();
public ResponseEntity<User> 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<User> create(@RequestBody User user) {
return new ResponseEntity<User>(this.repository.save(user), HttpStatus.CREATED);
}

@DeleteMapping("{id}")
public ResponseEntity<User> 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<User> 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<User>(this.repository.save(userToUpdate), HttpStatus.CREATED);
}
}
109 changes: 109 additions & 0 deletions src/main/java/com/booleanuk/api/model/Game.java
Original file line number Diff line number Diff line change
@@ -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 +
'}';
}
}
56 changes: 40 additions & 16 deletions src/main/java/com/booleanuk/api/model/User.java
Original file line number Diff line number Diff line change
@@ -1,35 +1,41 @@
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)
private Integer id;

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;
}
Expand All @@ -54,25 +60,41 @@ 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
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
Expand All @@ -81,7 +103,9 @@ public String toString() {
"id=" + id +
", email='" + email + '\'' +
", firstName='" + firstName + '\'' +
", isActive=" + isActive +
", lastName='" + lastName + '\'' +
", username='" + username + '\'' +
", phone='" + phone + '\'' +
'}';
}
}
Original file line number Diff line number Diff line change
@@ -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<Game, Integer> {

}
24 changes: 0 additions & 24 deletions src/main/resources/application.yml.example

This file was deleted.