j'ai mal à la tête mais ça marche

This commit is contained in:
Guams 2024-12-08 19:17:58 +01:00
parent 80a04c4614
commit fa920bd840
9 changed files with 152 additions and 41 deletions

View File

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

View File

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

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

View File

@ -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> {
}

View File

@ -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;

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

View File

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

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

View File

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