j'ai mal à la tête mais ça marche
This commit is contained in:
parent
80a04c4614
commit
fa920bd840
@ -0,0 +1,61 @@
|
|||||||
|
package com.guams.review.controller;
|
||||||
|
|
||||||
|
import com.guams.review.exception.NotFoundException;
|
||||||
|
import com.guams.review.modele.dao.Author;
|
||||||
|
import com.guams.review.service.AuthorService;
|
||||||
|
import com.guams.review.service.mapper.Mapper;
|
||||||
|
import com.guams.review.service.mapper.ReturnableAuthor;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import org.springframework.http.MediaType;
|
||||||
|
import org.springframework.util.Assert;
|
||||||
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@RequestMapping(path = "api/authors")
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
public class AuthorController {
|
||||||
|
|
||||||
|
private final AuthorService authorService;
|
||||||
|
private final Mapper mapper;
|
||||||
|
|
||||||
|
@GetMapping
|
||||||
|
public List<ReturnableAuthor> getUsers() {
|
||||||
|
return authorService.list();
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/{id}")
|
||||||
|
public ReturnableAuthor findUser(@PathVariable UUID id) {
|
||||||
|
Author author = authorService.findById(id).orElseThrow(() -> new NotFoundException("Author not found"));
|
||||||
|
return mapper.mapAuthor(author);
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostMapping
|
||||||
|
public ReturnableAuthor addUser(@RequestBody Author author) {
|
||||||
|
Assert.isNull(author.getId(), "Author id must be null");
|
||||||
|
return authorService.insert(author);
|
||||||
|
}
|
||||||
|
|
||||||
|
@PutMapping("/{id}")
|
||||||
|
public void updateUser(@PathVariable UUID id, @RequestBody Author updatedAuthor) {
|
||||||
|
Author authorToUpdate = authorService.findById(id).orElseThrow(() -> new NotFoundException("Author not found"));
|
||||||
|
authorService.insert(updatedAuthor.setId(authorToUpdate.getId()));
|
||||||
|
}
|
||||||
|
|
||||||
|
@PutMapping(value = "{id}/avatar", consumes = {MediaType.MULTIPART_FORM_DATA_VALUE})
|
||||||
|
public void updateUserAvatar(@PathVariable UUID id, @RequestPart MultipartFile avatar) throws IOException {
|
||||||
|
Author authorToUpdate = authorService.findById(id).orElseThrow(() -> new NotFoundException("Author not found"));
|
||||||
|
authorService.insert(authorToUpdate.setProfilePicture(avatar.getBytes()));
|
||||||
|
}
|
||||||
|
|
||||||
|
@DeleteMapping("/{id}")
|
||||||
|
public void deleteUser(@PathVariable UUID id) {
|
||||||
|
Author authorToDelete = authorService.findById(id).orElseThrow(() -> new NotFoundException("Author not found"));
|
||||||
|
authorService.delete(authorToDelete);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -1,14 +0,0 @@
|
|||||||
package com.guams.review.controller;
|
|
||||||
|
|
||||||
import com.guams.review.service.UserService;
|
|
||||||
import lombok.RequiredArgsConstructor;
|
|
||||||
import org.springframework.web.bind.annotation.*;
|
|
||||||
|
|
||||||
@RestController
|
|
||||||
@RequestMapping(path = "api/users")
|
|
||||||
@RequiredArgsConstructor
|
|
||||||
public class UserController {
|
|
||||||
|
|
||||||
private final UserService userService;
|
|
||||||
|
|
||||||
}
|
|
14
src/main/java/com/guams/review/modele/AuthorRepository.java
Normal file
14
src/main/java/com/guams/review/modele/AuthorRepository.java
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
package com.guams.review.modele;
|
||||||
|
|
||||||
|
import com.guams.review.modele.dao.Author;
|
||||||
|
import org.springframework.data.repository.CrudRepository;
|
||||||
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
@Repository
|
||||||
|
public interface AuthorRepository extends CrudRepository<Author, UUID> {
|
||||||
|
|
||||||
|
List<Author> findAll();
|
||||||
|
}
|
@ -1,9 +0,0 @@
|
|||||||
package com.guams.review.modele;
|
|
||||||
|
|
||||||
import com.guams.review.modele.dao.User;
|
|
||||||
import org.springframework.data.repository.CrudRepository;
|
|
||||||
import org.springframework.stereotype.Repository;
|
|
||||||
|
|
||||||
@Repository
|
|
||||||
public interface UserRepository extends CrudRepository<User, Long> {
|
|
||||||
}
|
|
@ -2,19 +2,23 @@ package com.guams.review.modele.dao;
|
|||||||
|
|
||||||
|
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
|
import lombok.Setter;
|
||||||
import lombok.experimental.Accessors;
|
import lombok.experimental.Accessors;
|
||||||
import org.springframework.data.annotation.Id;
|
import org.springframework.data.annotation.Id;
|
||||||
import org.springframework.data.relational.core.mapping.Column;
|
import org.springframework.data.relational.core.mapping.Column;
|
||||||
import org.springframework.data.relational.core.mapping.Table;
|
import org.springframework.data.relational.core.mapping.Table;
|
||||||
|
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
|
||||||
@Getter
|
@Getter
|
||||||
|
@Setter
|
||||||
@Accessors(chain = true)
|
@Accessors(chain = true)
|
||||||
@Table(name = "author")
|
@Table(name = "author")
|
||||||
public class User {
|
public class Author {
|
||||||
|
|
||||||
@Id
|
@Id
|
||||||
Long id;
|
UUID id;
|
||||||
|
|
||||||
@Column("name")
|
@Column("name")
|
||||||
String name;
|
String name;
|
38
src/main/java/com/guams/review/service/AuthorService.java
Normal file
38
src/main/java/com/guams/review/service/AuthorService.java
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
package com.guams.review.service;
|
||||||
|
|
||||||
|
import com.guams.review.modele.AuthorRepository;
|
||||||
|
import com.guams.review.modele.dao.Author;
|
||||||
|
import com.guams.review.service.mapper.Mapper;
|
||||||
|
import com.guams.review.service.mapper.ReturnableAuthor;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
public class AuthorService
|
||||||
|
{
|
||||||
|
private final Mapper mapper;
|
||||||
|
private final AuthorRepository authorRepository;
|
||||||
|
|
||||||
|
public List<ReturnableAuthor> list() {
|
||||||
|
return authorRepository.findAll().stream()
|
||||||
|
.map(mapper::mapAuthor)
|
||||||
|
.toList();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Optional<Author> findById(UUID id) {
|
||||||
|
return authorRepository.findById(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
public ReturnableAuthor insert(Author author) {
|
||||||
|
return mapper.mapAuthor(authorRepository.save(author));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void delete(Author author) {
|
||||||
|
authorRepository.delete(author);
|
||||||
|
}
|
||||||
|
}
|
@ -1,16 +0,0 @@
|
|||||||
package com.guams.review.service;
|
|
||||||
|
|
||||||
import com.guams.review.modele.dao.User;
|
|
||||||
import com.guams.review.modele.UserRepository;
|
|
||||||
import lombok.RequiredArgsConstructor;
|
|
||||||
import org.springframework.stereotype.Service;
|
|
||||||
|
|
||||||
import java.util.Optional;
|
|
||||||
|
|
||||||
@Service
|
|
||||||
@RequiredArgsConstructor
|
|
||||||
public class UserService
|
|
||||||
{
|
|
||||||
private final UserRepository userRepository;
|
|
||||||
|
|
||||||
}
|
|
17
src/main/java/com/guams/review/service/mapper/Mapper.java
Normal file
17
src/main/java/com/guams/review/service/mapper/Mapper.java
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
package com.guams.review.service.mapper;
|
||||||
|
|
||||||
|
import com.guams.review.modele.dao.Author;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
public class Mapper {
|
||||||
|
|
||||||
|
public Mapper() {}
|
||||||
|
|
||||||
|
public ReturnableAuthor mapAuthor(Author author) {
|
||||||
|
return new ReturnableAuthor()
|
||||||
|
.setId(author.getId())
|
||||||
|
.setName(author.getName())
|
||||||
|
.setProfilePicture(author.getProfilePicture());
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,16 @@
|
|||||||
|
package com.guams.review.service.mapper;
|
||||||
|
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.Setter;
|
||||||
|
import lombok.experimental.Accessors;
|
||||||
|
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
@Setter
|
||||||
|
@Accessors(chain = true)
|
||||||
|
public class ReturnableAuthor {
|
||||||
|
private UUID id;
|
||||||
|
private String name;
|
||||||
|
private byte[] profilePicture;
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user