diff --git a/.gitignore b/.gitignore index c2065bc..0696eb5 100644 --- a/.gitignore +++ b/.gitignore @@ -35,3 +35,6 @@ out/ ### VS Code ### .vscode/ +application.yml +*.jar +*.yml diff --git a/build.gradle b/build.gradle index fb070c4..fa7073a 100644 --- a/build.gradle +++ b/build.gradle @@ -1,33 +1,57 @@ plugins { - id 'java' - id 'org.springframework.boot' version '3.1.4' - id 'io.spring.dependency-management' version '1.1.3' + id 'java' + id 'org.springframework.boot' version '3.5.4' + id 'io.spring.dependency-management' version '1.1.7' } group = 'com.booleanuk' -version = '0.0.1' -sourceCompatibility = '17' +version = '0.2' +description = 'Games' + +java { + toolchain { + languageVersion = JavaLanguageVersion.of(21) + } +} configurations { - compileOnly { - extendsFrom annotationProcessor - } + compileOnly { + extendsFrom annotationProcessor + } } repositories { - mavenCentral() + mavenCentral() } dependencies { - implementation 'org.springframework.boot:spring-boot-starter-data-jpa' - implementation 'org.springframework.boot:spring-boot-starter-web' - compileOnly 'org.projectlombok:lombok' - developmentOnly 'org.springframework.boot:spring-boot-devtools' - implementation 'org.postgresql:postgresql:42.6.0' - annotationProcessor 'org.projectlombok:lombok' - testImplementation 'org.springframework.boot:spring-boot-starter-test' + implementation 'org.springframework.boot:spring-boot-starter-data-jpa' + implementation 'org.springframework.boot:spring-boot-starter-web' + developmentOnly 'org.springframework.boot:spring-boot-devtools' + testImplementation 'org.springframework.boot:spring-boot-starter-test' + testRuntimeOnly 'org.junit.platform:junit-platform-launcher' + implementation 'org.springframework.boot:spring-boot-starter-data-jpa' + implementation 'org.springframework.boot:spring-boot-starter-web' + compileOnly 'org.projectlombok:lombok' + developmentOnly 'org.springframework.boot:spring-boot-devtools' + implementation 'org.postgresql:postgresql:42.7.7' + annotationProcessor 'org.projectlombok:lombok' + testImplementation 'org.springframework.boot:spring-boot-starter-test' + testRuntimeOnly 'org.junit.platform:junit-platform-launcher' + } tasks.named('test') { - useJUnitPlatform() + useJUnitPlatform() +} + +jar { + duplicatesStrategy = DuplicatesStrategy.EXCLUDE + manifest { + attributes 'Main-Class': 'com.booleanuk.Employee.Main' + } + + from { + configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) } + } } diff --git a/dockers/Dockerfile b/dockers/Dockerfile new file mode 100644 index 0000000..c28337f --- /dev/null +++ b/dockers/Dockerfile @@ -0,0 +1,9 @@ +FROM mcr.microsoft.com/openjdk/jdk:21-ubuntu + +WORKDIR /app + +COPY java.docker.day.2-0.0.1.jar /app/java.docker.day.2-0.0.1.jar + +EXPOSE 4000 + +ENTRYPOINT [ "java", "-jar", "java.docker.day.2-0.0.1.jar" ] \ No newline at end of file diff --git a/src/main/java/com/booleanuk/api/Main.java b/src/main/java/com/booleanuk/api/Main.java new file mode 100644 index 0000000..8e749e0 --- /dev/null +++ b/src/main/java/com/booleanuk/api/Main.java @@ -0,0 +1,11 @@ +package com.booleanuk.api; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class Main { + public static void main(String[] args) { + SpringApplication.run(Main.class, args); + } +} 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..b10bfea --- /dev/null +++ b/src/main/java/com/booleanuk/api/controller/GameController.java @@ -0,0 +1,62 @@ +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 GameRepository gameRepository; + + @GetMapping + public ResponseEntity> getAll() { + return ResponseEntity.ok(this.gameRepository.findAll()); + } + + @GetMapping("{id}") + public ResponseEntity getOne(@PathVariable int id) { + Game game = null; + game = this.gameRepository.findById(id).orElseThrow(() -> + new ResponseStatusException(HttpStatus.NOT_FOUND, "Game with given id not found")); + return ResponseEntity.ok(game); // 200 status by default + } + + @PostMapping + public ResponseEntity create(@RequestBody Game game) { + return new ResponseEntity(this.gameRepository.save(game), HttpStatus.CREATED); // 201 status + } + + @PutMapping("{id}") + public ResponseEntity update(@PathVariable int id, @RequestBody Game game) { + Game gameToUpdate = this.gameRepository.findById(id).orElseThrow(() -> + new ResponseStatusException(HttpStatus.NOT_FOUND, "Game with given id not found")); + + gameToUpdate.setTitle(game.getTitle()); + gameToUpdate.setGenre(game.getGenre()); + gameToUpdate.setPublisher(game.getPublisher()); + gameToUpdate.setDeveloper(game.getDeveloper()); + gameToUpdate.setReleaseYear(game.getReleaseYear()); + gameToUpdate.setEarlyAccess(game.isEarlyAccess()); + + return new ResponseEntity(this.gameRepository.save(gameToUpdate), HttpStatus.CREATED); + } + + @DeleteMapping("{id}") + public ResponseEntity delete (@PathVariable int id) { + Game gameToDelete = this.gameRepository.findById(id).orElseThrow(() -> + new ResponseStatusException(HttpStatus.NOT_FOUND, "Game with given id not found")); + + this.gameRepository.delete(gameToDelete); + + return ResponseEntity.ok(gameToDelete); // 200 status by default + } +} diff --git a/src/main/java/com/booleanuk/api/controller/UserController.java b/src/main/java/com/booleanuk/api/controller/UserController.java new file mode 100644 index 0000000..5084e96 --- /dev/null +++ b/src/main/java/com/booleanuk/api/controller/UserController.java @@ -0,0 +1,64 @@ +package com.booleanuk.api.controller; + +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.EnumMap; +import java.util.List; + +@RestController +@RequestMapping("users") +public class UserController { + + @Autowired + private UserRepository repository; + + @GetMapping + public ResponseEntity> getAll() { + return ResponseEntity.ok(this.repository.findAll()); + } + + @GetMapping("{id}") + public ResponseEntity getById(@PathVariable("id") int id) { + User user = null; + user = this.repository.findById(id).orElseThrow(() -> + new ResponseStatusException(HttpStatus.NOT_FOUND, "User with given id not found")); + + return ResponseEntity.ok(user); + } + + @PostMapping + public ResponseEntity create(@RequestBody User employee) { + return new ResponseEntity(this.repository.save(employee), HttpStatus.CREATED); // 201 status + } + + @PutMapping("{id}") + public ResponseEntity update(@PathVariable int id, @RequestBody User user) { + User userToUpdate = this.repository.findById(id).orElseThrow(() -> + new ResponseStatusException(HttpStatus.NOT_FOUND, "User with given id not found")); + + userToUpdate.setFirstName(user.getFirstName()); + userToUpdate.setLastName(user.getLastName()); + userToUpdate.setPhone(user.getPhone()); + userToUpdate.setEmail(user.getEmail()); + userToUpdate.setUserName(user.getUserName()); + + return new ResponseEntity(this.repository.save(userToUpdate), HttpStatus.CREATED); + } + + @DeleteMapping("{id}") + public ResponseEntity delete (@PathVariable int id) { + User userToDelete = this.repository.findById(id).orElseThrow(() -> + new ResponseStatusException(HttpStatus.NOT_FOUND, "Employee with given id not found")); + + this.repository.delete(userToDelete); + + return ResponseEntity.ok(userToDelete); // 200 status by default + } + +} 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..e8a2758 --- /dev/null +++ b/src/main/java/com/booleanuk/api/model/Game.java @@ -0,0 +1,58 @@ +package com.booleanuk.api.model; + +import jakarta.persistence.*; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; + +@Getter +@Setter +@NoArgsConstructor +@Entity +@Table(name = "games") +public class Game { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private int id; + + @Column(name = "title") + private String title; + + @Column(name = "genre") + private String genre; + + @Column(name = "publisher") + private String publisher; + + @Column(name = "developer") + private String developer; + + @Column(name = "release_year") + private int releaseYear; + + @Column(name = "is_early_access") + private boolean isEarlyAccess; + + public Game (int 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; + } + + @Override + public String toString() { + return "Game{" + + "id=" + id + + ", title='" + title + '\'' + + ", genre='" + genre + '\'' + + ", publisher='" + publisher + '\'' + + ", developer='" + developer + '\'' + + ", releaseYear=" + releaseYear + + ", isEarlyAccess=" + isEarlyAccess + + '}'; + } +} diff --git a/src/main/java/com/booleanuk/api/model/User.java b/src/main/java/com/booleanuk/api/model/User.java new file mode 100644 index 0000000..3175860 --- /dev/null +++ b/src/main/java/com/booleanuk/api/model/User.java @@ -0,0 +1,74 @@ +package com.booleanuk.api.model; + +import jakarta.persistence.*; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; + +import java.util.Objects; + +@Getter +@Setter +@NoArgsConstructor +@Entity +@Table(name = "Users") +public class User { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Integer id; + + @Column(name = "email_address") + private String email; + + @Column(name = "first_name") + private String firstName; + + @Column(name = "last_name") + private String lastName; + + @Column(name = "user_name") + private String userName; + + @Column(name = "phone") + private String phone; + + public User(Integer id, String email, String firstName, String lastName, String userName, String phone) { + this.id = id; + this.email = email; + this.firstName = firstName; + this.lastName = lastName; + this.userName = userName; + this.phone = phone; + } + + public User(String email, String firstName) { + this.email = email; + this.firstName = firstName; + } + + @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(lastName, user.lastName) && + Objects.equals(userName, user.userName) && Objects.equals(phone, user.phone); + } + + @Override + public int hashCode() { + return Objects.hash(id, email, firstName, lastName, userName, phone); + } + + @Override + public String toString() { + return "User{" + + "id=" + id + + ", email='" + email + '\'' + + ", firstName='" + firstName + '\'' + + ", 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..82ad359 --- /dev/null +++ b/src/main/java/com/booleanuk/api/repository/GameRepository.java @@ -0,0 +1,6 @@ +package com.booleanuk.api.repository; + +import com.booleanuk.api.model.Game; +import org.springframework.data.jpa.repository.JpaRepository; + +public interface GameRepository extends JpaRepository { } \ No newline at end of file diff --git a/src/main/java/com/booleanuk/api/repository/UserRepository.java b/src/main/java/com/booleanuk/api/repository/UserRepository.java new file mode 100644 index 0000000..cf18a2e --- /dev/null +++ b/src/main/java/com/booleanuk/api/repository/UserRepository.java @@ -0,0 +1,6 @@ +package com.booleanuk.api.repository; + +import com.booleanuk.api.model.User; +import org.springframework.data.jpa.repository.JpaRepository; + +public interface UserRepository extends JpaRepository { }