diff --git a/.gitignore b/.gitignore index c2065bc..b816e8a 100644 --- a/.gitignore +++ b/.gitignore @@ -35,3 +35,7 @@ out/ ### VS Code ### .vscode/ + +*.yml +*.jar +application.yml \ No newline at end of file diff --git a/build.gradle b/build.gradle index fb070c4..28b595d 100644 --- a/build.gradle +++ b/build.gradle @@ -5,7 +5,7 @@ plugins { } group = 'com.booleanuk' -version = '0.0.1' +version = '0.0.3' sourceCompatibility = '17' configurations { @@ -19,15 +19,40 @@ repositories { } 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' + 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..3633090 --- /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.3.jar /app/java.docker.day.2-0.0.3.jar + +EXPOSE 4000 + +ENTRYPOINT [ "java", "-jar", "java.docker.day.2-0.0.3.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..e6436b8 --- /dev/null +++ b/src/main/java/com/booleanuk/api/Main.java @@ -0,0 +1,12 @@ +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..c2933b9 --- /dev/null +++ b/src/main/java/com/booleanuk/api/controller/GameController.java @@ -0,0 +1,75 @@ +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 get(@PathVariable int id){ + Game employee = this.gameRepository.findById(id).orElseThrow( + () -> new ResponseStatusException(HttpStatus.NOT_FOUND, "This is not found by dave!")); + return ResponseEntity.ok(employee); + } + + @PostMapping + public ResponseEntity add(@RequestBody Game employee){ + return new ResponseEntity<>(this.gameRepository.save(employee), HttpStatus.CREATED); + } + + @PutMapping("/{id}") + public ResponseEntity update( @PathVariable int id,@RequestBody Game employee){ + Game empToUpdate = this.gameRepository.findById(id).orElseThrow( + () -> new ResponseStatusException(HttpStatus.NOT_FOUND, "This is not found by dave!")); + empToUpdate.setTitle(employee.getTitle()); + empToUpdate.setGenre(employee.getGenre()); + empToUpdate.setPublisher(employee.getPublisher()); + empToUpdate.setDeveloper(employee.getDeveloper()); + empToUpdate.setReleaseYear(employee.getReleaseYear()); + empToUpdate.setIsEarlyAccess(employee.getIsEarlyAccess()); + + return new ResponseEntity<>(this.gameRepository.save(empToUpdate), HttpStatus.CREATED); + } + + @DeleteMapping("/{id}") + public ResponseEntity delete(@PathVariable int id){ + Game empToBeDeleted = this.gameRepository.findById(id).orElseThrow( + () -> new ResponseStatusException(HttpStatus.NOT_FOUND, "This is not found by dave!")); + this.gameRepository.delete(empToBeDeleted); + return ResponseEntity.ok(empToBeDeleted); + } + + +// @GetMapping("{id}") +// public Game getById(@PathVariable("id") Integer id) { +// return this.gameRepository.findById(id).orElseThrow(); +// } +// +// record PostGame(String email, String genre) {} +// +// @ResponseStatus(HttpStatus.CREATED) +// @PostMapping +// public Game create(@RequestBody PostGame request) { +// Game user = new Game(request.email(), request.genre()); +// return this.gameRepository.save(user); +// } +} 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..e8f76a7 --- /dev/null +++ b/src/main/java/com/booleanuk/api/controller/UserController.java @@ -0,0 +1,72 @@ +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.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 get(@PathVariable int id){ + User employee = this.repository.findById(id).orElseThrow( + () -> new ResponseStatusException(HttpStatus.NOT_FOUND, "This is not found by dave!")); + return ResponseEntity.ok(employee); + } + + @PostMapping + public ResponseEntity add(@RequestBody User employee){ + return new ResponseEntity<>(this.repository.save(employee), HttpStatus.CREATED); + } + + @PutMapping("/{id}") + public ResponseEntity update( @PathVariable int id,@RequestBody User employee){ + User empToUpdate = this.repository.findById(id).orElseThrow( + () -> new ResponseStatusException(HttpStatus.NOT_FOUND, "This is not found by dave!")); + empToUpdate.setEmail(employee.getEmail()); + empToUpdate.setFirstName(employee.getFirstName()); + empToUpdate.setIsActive(employee.getIsActive()); + + return new ResponseEntity<>(this.repository.save(empToUpdate), HttpStatus.CREATED); + } + + @DeleteMapping("/{id}") + public ResponseEntity delete(@PathVariable int id){ + User empToBeDeleted = this.repository.findById(id).orElseThrow( + () -> new ResponseStatusException(HttpStatus.NOT_FOUND, "This is not found by dave!")); + this.repository.delete(empToBeDeleted); + return ResponseEntity.ok(empToBeDeleted); + } + + +// @GetMapping("{id}") +// public User getById(@PathVariable("id") Integer id) { +// return this.repository.findById(id).orElseThrow(); +// } +// +// record PostUser(String email, String genre) {} +// +// @ResponseStatus(HttpStatus.CREATED) +// @PostMapping +// public User create(@RequestBody PostUser request) { +// User user = new User(request.email(), request.genre()); +// return this.repository.save(user); +// } +} 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..68de512 --- /dev/null +++ b/src/main/java/com/booleanuk/api/model/Game.java @@ -0,0 +1,65 @@ +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="Games") +public class Game { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Integer id; + @Column + private String title; + @Column + private String genre; + @Column + private String publisher; + @Column + private String developer; + @Column + private int releaseYear; + @Column + 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; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + Game user = (Game) o; + return Objects.equals(id, user.id) && Objects.equals(title, user.title) && Objects.equals(genre, user.genre) && Objects.equals(isEarlyAccess, user.isEarlyAccess); + } + + @Override + public int hashCode() { + return Objects.hash(id, title, genre, isEarlyAccess); + } + + @Override + public String toString() { + return "User{" + + "id=" + id + + ", title='" + title + '\'' + + ", genre='" + genre + '\'' + + ", 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..2c02202 --- /dev/null +++ b/src/main/java/com/booleanuk/api/model/User.java @@ -0,0 +1,60 @@ +package com.booleanuk.api.model; + +import jakarta.persistence.*; +import lombok.*; + +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 + private String firstName; + @Column + private Boolean isActive; + + + public User(Integer id, String email, String firstName, Boolean isActive) { + this.id = id; + this.email = email; + this.firstName = firstName; + this.isActive = isActive; + } + + public User(String email, String firstName) { + this.email = email; + this.firstName = firstName; + this.isActive = false; + } + + @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); + } + + @Override + public int hashCode() { + return Objects.hash(id, email, firstName, isActive); + } + + @Override + public String toString() { + return "User{" + + "id=" + id + + ", email='" + email + '\'' + + ", genre='" + firstName + '\'' + + ", isEarlyAccess=" + isActive + + '}'; + } +} 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/java/com/booleanuk/api/repository/UserRepository.java b/src/main/java/com/booleanuk/api/repository/UserRepository.java new file mode 100644 index 0000000..2d7d82f --- /dev/null +++ b/src/main/java/com/booleanuk/api/repository/UserRepository.java @@ -0,0 +1,8 @@ +package com.booleanuk.api.repository; + +import com.booleanuk.api.model.User; +import org.springframework.data.jpa.repository.JpaRepository; + +public interface UserRepository extends JpaRepository { + +}