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

Iris Flystam #13

Open
wants to merge 4 commits 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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
The diff you're trying to view is too large. We only load the first 3000 changed files.
2 changes: 1 addition & 1 deletion build.gradle → backend-java/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ plugins {
}

group = 'com.booleanuk'
version = '0.0.1-SNAPSHOT'
version = '0.0.1'

java {
toolchain {
Expand Down
0 gradlew → backend-java/gradlew
100755 → 100644
File renamed without changes.
File renamed without changes.
File renamed without changes.
13 changes: 13 additions & 0 deletions backend-java/src/main/java/com/booleanuk/simpleapi/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.booleanuk.simpleapi;

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);
}

}
18 changes: 18 additions & 0 deletions backend-java/src/main/java/com/booleanuk/simpleapi/WebConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.booleanuk.simpleapi;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

//@Configuration
//public class WebConfig implements WebMvcConfigurer {
//
// @Override
// public void addCorsMappings(CorsRegistry registry) {
// registry.addMapping("/**")
// .allowedOrigins("http://localhost:5173")
// .allowedMethods("GET", "POST", "PUT", "DELETE")
// .allowedHeaders("*")
// .allowCredentials(false);
// }
//}
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
package com.booleanuk.simpleapi.controller;

import com.booleanuk.simpleapi.model.*;
import com.booleanuk.simpleapi.repository.BorrowableRepository;
import com.booleanuk.simpleapi.response.ApiResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.Optional;

@CrossOrigin(origins = "http://localhost:5173")
@RestController
@RequestMapping("library")
public class Controller {
@Autowired
private BorrowableRepository repository;

@PostMapping("book")
public ResponseEntity<ApiResponse<?>> createBook (@RequestBody Book bookDetails) {
if (!bookDetails.isValid()) {
ApiResponse<String> response = new ApiResponse<>("error", "Could not create the specified book, please check that all fields are correct.");
return new ResponseEntity<>(response, HttpStatus.BAD_REQUEST);
}

Book book = new Book(bookDetails.getTitle(), bookDetails.getAuthor(), bookDetails.isBorrowed());

ApiResponse<Book> response = new ApiResponse<>("success", repository.save(book));
return new ResponseEntity<>(response, HttpStatus.CREATED);
}

@PostMapping("/{id}/borrow")
public ResponseEntity<ApiResponse<?>> borrowItem(@PathVariable int id) {
Optional<Book> boo = this.repository.findById(id);
if (boo.isEmpty()) {
ApiResponse<String> response = new ApiResponse<>("error", String.format("No item with id %d found.", id));
return new ResponseEntity<>(response, HttpStatus.NOT_FOUND);
}
Book book = boo.get();
if (book.isBorrowed()) {
ApiResponse<String> response = new ApiResponse<>("error", String.format("Item with id %d is currently borrowed already.", id));
return new ResponseEntity<>(response, HttpStatus.FORBIDDEN);
}
book.setBorrowed(true);

this.repository.save(book);

ApiResponse<Book> response = new ApiResponse<>("success", book);
return new ResponseEntity<>(response, HttpStatus.OK);
}

@PostMapping("/{id}/return")
public ResponseEntity<ApiResponse<?>> returnItem(@PathVariable int id) {
Optional<Book> boo = this.repository.findById(id);
if (boo.isEmpty()) {
ApiResponse<String> response = new ApiResponse<>("error", String.format("No item with id %d found.", id));
return new ResponseEntity<>(response, HttpStatus.NOT_FOUND);
}
Book book = boo.get();
if (!book.isBorrowed()) {
ApiResponse<String> response = new ApiResponse<>("error", String.format("Item with id %d is currently borrowed already.", id));
return new ResponseEntity<>(response, HttpStatus.FORBIDDEN);
}
book.setBorrowed(false);

this.repository.save(book);

ApiResponse<Book> response = new ApiResponse<>("success", book);
return new ResponseEntity<>(response, HttpStatus.OK);
}

@GetMapping
public ResponseEntity<ApiResponse<List<Book>>> readAll() {
List<Book> books = this.repository.findAll();
ApiResponse<List<Book>> response = new ApiResponse<>("success", books);
return new ResponseEntity<>(response, HttpStatus.OK);
}

@GetMapping("{id}")
public ResponseEntity<ApiResponse<?>> readBook(@PathVariable int id) {
Optional<Book> boo = this.repository.findById(id);

if (boo.isEmpty()) {
ApiResponse<String> response = new ApiResponse<>("error", String.format("No item with id %d found.", id));
return new ResponseEntity<>(response, HttpStatus.NOT_FOUND);
}

Book book = boo.get();
ApiResponse<Book> response = new ApiResponse<>("success", repository.save(book));
return new ResponseEntity<>(response, HttpStatus.OK);
}

@PutMapping("{id}")
public ResponseEntity<ApiResponse<?>> updateBook (@PathVariable int id, @RequestBody Book bookDetails) {
Optional<Book> boo = this.repository.findById(id);

if (boo.isEmpty()) {
ApiResponse<String> response = new ApiResponse<>("error", String.format("No book with id %d found.", id));
return new ResponseEntity<>(response, HttpStatus.NOT_FOUND);
}
if (!bookDetails.isValid()) {
ApiResponse<String> response = new ApiResponse<>("error", "Could not create the specified book, please check that all fields are correct.");
return new ResponseEntity<>(response, HttpStatus.BAD_REQUEST);
}

Book book = boo.get();
book.setTitle(bookDetails.getTitle());
book.setAuthor(bookDetails.getAuthor());
book.setBorrowed(bookDetails.isBorrowed());

ApiResponse<Book> response = new ApiResponse<>("success", repository.save(book));
return new ResponseEntity<>(response, HttpStatus.CREATED);
}

@DeleteMapping("{id}")
public ResponseEntity<ApiResponse<?>> delete (@PathVariable int id) {
Optional<Book> boo = this.repository.findById(id);

if (boo.isEmpty()) {
ApiResponse<String> response = new ApiResponse<>("error", String.format("No item with id %d found.", id));
return new ResponseEntity<>(response, HttpStatus.NOT_FOUND);
}

Book book = boo.get();
repository.delete(book);
ApiResponse<Book> response = new ApiResponse<>("success", book);
return new ResponseEntity<>(response, HttpStatus.OK);
}
}
65 changes: 65 additions & 0 deletions backend-java/src/main/java/com/booleanuk/simpleapi/model/Book.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package com.booleanuk.simpleapi.model;

import com.fasterxml.jackson.annotation.JsonIgnore;
import io.micrometer.common.util.StringUtils;
import jakarta.persistence.*;
import lombok.*;

import java.util.Objects;

@Data
@NoArgsConstructor
@Entity
@Table(name = "books")
@Getter
@Setter
public class Book {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
protected int id;

@Column(name = "title")
private String title;

@Column(name = "author")
private String author;

@Column(name = "is_borrowed")
private boolean isBorrowed;

public Book(String title, String author, boolean isBorrowed) {
this.title = title;
this.author = author;
this.isBorrowed = isBorrowed;
}

public Book(int id) {
this.id = id;
}

@JsonIgnore
public boolean isValid() {
return !(StringUtils.isBlank(title)
|| StringUtils.isBlank(author));
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
Book book = (Book) o;
return Objects.equals(id, book.id)
&& Objects.equals(title, book.title)
&& Objects.equals(author, book.author)
&& Objects.equals(isBorrowed, book.isBorrowed);
}

@Override
public int hashCode() {
return Objects.hash(id, title, author, isBorrowed);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.booleanuk.simpleapi.repository;

import com.booleanuk.simpleapi.model.Book;
import org.springframework.data.jpa.repository.JpaRepository;

public interface BorrowableRepository extends JpaRepository<Book, Integer> {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.booleanuk.simpleapi.response;

import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class ApiResponse<T> {
private String status;
private T data;

public ApiResponse(String status, T data) {
this.status = status;
this.data = data;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class SimpleApiApplicationTests {
class SimpleMainTests {

@Test
void contextLoads() {
Expand Down
Loading