Skip to content

Commit

Permalink
Complete service layers task
Browse files Browse the repository at this point in the history
  • Loading branch information
JackKaif committed Feb 20, 2024
1 parent f883ffd commit ab36432
Show file tree
Hide file tree
Showing 9 changed files with 166 additions and 11 deletions.
2 changes: 1 addition & 1 deletion java-spring-ru/service-layer/settings.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
rootProject.name = "one-to-many"
rootProject.name = "service-layer"
// spring.jpa.generate-ddl = true
// spring.jpa.hibernate.ddl-auto
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import exercise.service.AuthorService;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.GetMapping;
Expand All @@ -17,6 +18,7 @@
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.http.HttpStatus;

import java.net.URI;
import java.util.List;

@RestController
Expand All @@ -27,6 +29,39 @@ public class AuthorsController {
private AuthorService authorService;

// BEGIN

@GetMapping("")
public ResponseEntity<List<AuthorDTO>> index() {
var authors = authorService.getAll();
return ResponseEntity.ok()
.body(authors);
}

@GetMapping("/{id}")
public ResponseEntity<AuthorDTO> show(@PathVariable Long id) {
var author = authorService.findById(id);
return ResponseEntity.ok()
.body(author);
}

@PostMapping("")
public ResponseEntity<AuthorDTO> create(@RequestBody AuthorCreateDTO newAuthor) {
var author = authorService.create(newAuthor);
return ResponseEntity.created(URI.create("/authors"))
.body(author);
}

@PutMapping("/{id}")
public ResponseEntity<AuthorDTO> update(@PathVariable Long id,
@RequestBody AuthorUpdateDTO editedAuthor) {
var author = authorService.update(id, editedAuthor);
return ResponseEntity.ok()
.body(author);
}

@DeleteMapping("/{id}")
public ResponseEntity<?> delete(@PathVariable Long id) {
authorService.delete(id);
return ResponseEntity.ok().build();
}
// END
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package exercise.controller;

import java.net.URI;
import java.util.List;

import exercise.dto.BookCreateDTO;
Expand All @@ -8,6 +9,7 @@
import exercise.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
Expand All @@ -26,6 +28,39 @@ public class BooksController {
private BookService bookService;

// BEGIN

@GetMapping("")
public ResponseEntity<List<BookDTO>> index() {
var books = bookService.getAll();
return ResponseEntity.ok()
.body(books);
}

@GetMapping("/{id}")
public ResponseEntity<BookDTO> show(@PathVariable Long id) {
var book = bookService.findById(id);
return ResponseEntity.ok()
.body(book);
}

@PostMapping("")
public ResponseEntity<BookDTO> create(@RequestBody BookCreateDTO newBook) {
var book = bookService.create(newBook);
return ResponseEntity.created(URI.create("/books"))
.body(book);
}

@PutMapping("/{id}")
public ResponseEntity<BookDTO> update(@PathVariable Long id,
@RequestBody BookUpdateDTO editedBook) {
var book = bookService.update(id, editedBook);
return ResponseEntity.ok()
.body(book);
}

@DeleteMapping("/{id}")
public ResponseEntity<?> delete(@PathVariable Long id) {
bookService.delete(id);
return ResponseEntity.ok().build();
}
// END
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
public abstract class AuthorMapper {

// BEGIN

public abstract Author map(AuthorCreateDTO dto);

public abstract AuthorDTO map(Author model);
// END

public abstract void update(AuthorUpdateDTO dto, @MappingTarget Author model);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,13 @@
public abstract class BookMapper {

// BEGIN

@Mapping(target = "author", source = "authorId")
public abstract Book map(BookCreateDTO dto);

@Mapping(target = "authorId", source = "author.id")
@Mapping(target = "authorFirstName", source = "author.firstName")
@Mapping(target = "authorLastName", source = "author.lastName")
public abstract BookDTO map(Book model);
// END

@Mapping(target = "author", source = "authorId")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,51 @@
import exercise.repository.AuthorRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.List;

@Service
public class AuthorService {
// BEGIN

@Autowired
private AuthorRepository authorRepository;

@Autowired
private AuthorMapper authorMapper;

public List<AuthorDTO> getAll() {
return authorRepository.findAll().stream()
.map(authorMapper::map)
.toList();
}

public AuthorDTO create(AuthorCreateDTO newAuthor) {
var author = authorMapper.map(newAuthor);
authorRepository.save(author);
return authorMapper.map(author);
}

public AuthorDTO findById(Long id) {
var author = authorRepository.findById(id)
.orElseThrow(() -> {
throw new ResourceNotFoundException("Author with id " + id + " not found");
});
return authorMapper.map(author);
}

public AuthorDTO update(Long id, AuthorUpdateDTO editedAuthor) {
var author = authorRepository.findById(id)
.orElseThrow(() -> {
throw new ResourceNotFoundException("Author with id " + id + " not found");
});
authorMapper.update(editedAuthor, author);
authorRepository.save(author);
return authorMapper.map(author);
}

public void delete(Long id) {
authorRepository.deleteById(id);
}
// END
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package exercise.service;

import exercise.dto.BookCreateDTO;
import exercise.dto.BookDTO;
import exercise.dto.BookUpdateDTO;
import exercise.dto.*;
import exercise.exception.ResourceNotFoundException;
import exercise.mapper.BookMapper;
import exercise.repository.BookRepository;
Expand All @@ -14,6 +12,44 @@
@Service
public class BookService {
// BEGIN

@Autowired
private BookRepository bookRepository;

@Autowired
private BookMapper bookMapper;

public List<BookDTO> getAll() {
return bookRepository.findAll().stream()
.map(bookMapper::map)
.toList();
}

public BookDTO create(BookCreateDTO newBook) {
var book = bookMapper.map(newBook);
bookRepository.save(book);
return bookMapper.map(book);
}

public BookDTO findById(Long id) {
var book = bookRepository.findById(id)
.orElseThrow(() -> {
throw new ResourceNotFoundException("Book with id " + id + " not found");
});
return bookMapper.map(book);
}

public BookDTO update(Long id, BookUpdateDTO editedBook) {
var book = bookRepository.findById(id)
.orElseThrow(() -> {
throw new ResourceNotFoundException("Book with id " + id + " not found");
});
bookMapper.update(editedBook, book);
bookRepository.save(book);
return bookMapper.map(book);
}

public void delete(Long id) {
bookRepository.deleteById(id);
}
// END
}
Original file line number Diff line number Diff line change
Expand Up @@ -156,9 +156,10 @@ public void testPartialUpdate() throws Exception {
assertThat(author.getFirstName()).isEqualTo(dto.get("firstName"));
}

@Test
public void testDestroy() throws Exception {
authorRepository.save(testAuthor);
var request = delete("/books/{id}", testAuthor.getId());
var request = delete("/authors/{id}", testAuthor.getId());
mockMvc.perform(request)
.andExpect(status().isOk());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ public void testPartialUpdate() throws Exception {
assertThat(task.getAuthor().getId()).isEqualTo(dto.get("authorId"));
}

@Test
public void testDestroy() throws Exception {
bookRepository.save(testBook);
var request = delete("/books/{id}", testBook.getId());
Expand Down

0 comments on commit ab36432

Please sign in to comment.