-
Notifications
You must be signed in to change notification settings - Fork 256
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add personage management functionality
Introduced core classes, services, and configurations necessary for handling personages in the application. Includes creating, updating, retrieving, and deleting personages, as well as exception handling and data loading integrations.
- Loading branch information
Showing
19 changed files
with
477 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
src/main/java/mate/academy/rickandmorty/config/DataLoadConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package mate.academy.rickandmorty.config; | ||
|
||
import java.util.List; | ||
import lombok.RequiredArgsConstructor; | ||
import mate.academy.rickandmorty.dto.external.PersonageDataDto; | ||
import mate.academy.rickandmorty.mapper.PersonageMapper; | ||
import mate.academy.rickandmorty.model.Personage; | ||
import mate.academy.rickandmorty.service.PersonageClient; | ||
import mate.academy.rickandmorty.service.PersonageService; | ||
import org.springframework.boot.ApplicationRunner; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
@Configuration | ||
@RequiredArgsConstructor | ||
public class DataLoadConfiguration { | ||
private final PersonageClient personageClient; | ||
private final PersonageService personageService; | ||
private final PersonageMapper personageMapper; | ||
|
||
@Bean | ||
public ApplicationRunner loadDataAtStartup() { | ||
return args -> { | ||
List<PersonageDataDto> charactersData = personageClient.fetchPersonages(); | ||
List<Personage> personages = charactersData.stream() | ||
.map(personageMapper::toModelFromMeteData) | ||
.toList(); | ||
personageService.savePersonages(personages); | ||
}; | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
src/main/java/mate/academy/rickandmorty/config/MapperConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package mate.academy.rickandmorty.config; | ||
|
||
import org.mapstruct.InjectionStrategy; | ||
import org.mapstruct.NullValueCheckStrategy; | ||
|
||
@org.mapstruct.MapperConfig( | ||
componentModel = "spring", | ||
injectionStrategy = InjectionStrategy.CONSTRUCTOR, | ||
nullValueCheckStrategy = NullValueCheckStrategy.ALWAYS, | ||
implementationPackage = "<PACKAGE_NAME>.impl" | ||
) | ||
public class MapperConfig { | ||
} |
53 changes: 53 additions & 0 deletions
53
src/main/java/mate/academy/rickandmorty/controller/PersonageController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package mate.academy.rickandmorty.controller; | ||
|
||
import jakarta.validation.Valid; | ||
import java.util.List; | ||
import lombok.RequiredArgsConstructor; | ||
import mate.academy.rickandmorty.dto.internal.CreatePersonageRequestDto; | ||
import mate.academy.rickandmorty.dto.internal.PersonageDto; | ||
import mate.academy.rickandmorty.service.PersonageService; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.web.bind.annotation.DeleteMapping; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.PutMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.ResponseStatus; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
@RequestMapping("/personages") | ||
@RequiredArgsConstructor | ||
public class PersonageController { | ||
private final PersonageService personageService; | ||
|
||
@GetMapping | ||
public List<PersonageDto> getAll() { | ||
return personageService.findAll(); | ||
} | ||
|
||
@GetMapping("/{id}") | ||
public PersonageDto getPersonageById(@PathVariable Long id) { | ||
return personageService.findById(id); | ||
} | ||
|
||
@ResponseStatus(HttpStatus.CREATED) | ||
@PostMapping | ||
public PersonageDto createPersonage(@RequestBody @Valid CreatePersonageRequestDto requestDto) { | ||
return personageService.save(requestDto); | ||
} | ||
|
||
@PutMapping("/{id}") | ||
public PersonageDto updatePersonage(@PathVariable Long id, | ||
@RequestBody @Valid CreatePersonageRequestDto requestDto) { | ||
return personageService.updateById(id, requestDto); | ||
} | ||
|
||
@ResponseStatus(HttpStatus.NO_CONTENT) | ||
@DeleteMapping("/{id}") | ||
public void deletePersonage(@PathVariable Long id) { | ||
personageService.deleteById(id); | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
src/main/java/mate/academy/rickandmorty/dto/external/PersonageDataDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package mate.academy.rickandmorty.dto.external; | ||
|
||
public record PersonageDataDto( | ||
String id, | ||
String name, | ||
String status, | ||
String gender) { | ||
} |
9 changes: 9 additions & 0 deletions
9
src/main/java/mate/academy/rickandmorty/dto/external/PersonageResponseDataDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package mate.academy.rickandmorty.dto.external; | ||
|
||
import java.util.List; | ||
import lombok.Data; | ||
|
||
@Data | ||
public class PersonageResponseDataDto { | ||
private List<PersonageDataDto> results; | ||
} |
18 changes: 18 additions & 0 deletions
18
src/main/java/mate/academy/rickandmorty/dto/internal/CreatePersonageRequestDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package mate.academy.rickandmorty.dto.internal; | ||
|
||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; | ||
import jakarta.validation.constraints.NotBlank; | ||
import lombok.Data; | ||
|
||
@Data | ||
@JsonIgnoreProperties(ignoreUnknown = true) | ||
public class CreatePersonageRequestDto { | ||
@NotBlank(message = "externalId is mandatory") | ||
private String externalId; | ||
@NotBlank(message = "name is mandatory") | ||
private String name; | ||
@NotBlank(message = "status is mandatory") | ||
private String status; | ||
@NotBlank(message = "gender is mandatory") | ||
private String gender; | ||
} |
12 changes: 12 additions & 0 deletions
12
src/main/java/mate/academy/rickandmorty/dto/internal/PersonageDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package mate.academy.rickandmorty.dto.internal; | ||
|
||
import lombok.Data; | ||
|
||
@Data | ||
public class PersonageDto { | ||
private Long id; | ||
private String externalId; | ||
private String name; | ||
private String status; | ||
private String gender; | ||
} |
49 changes: 49 additions & 0 deletions
49
src/main/java/mate/academy/rickandmorty/exception/CustomGlobalExceptionHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package mate.academy.rickandmorty.exception; | ||
|
||
import java.time.LocalDateTime; | ||
import java.util.LinkedHashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.HttpStatusCode; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.lang.NonNull; | ||
import org.springframework.validation.FieldError; | ||
import org.springframework.validation.ObjectError; | ||
import org.springframework.web.bind.MethodArgumentNotValidException; | ||
import org.springframework.web.bind.annotation.ControllerAdvice; | ||
import org.springframework.web.context.request.WebRequest; | ||
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler; | ||
|
||
@ControllerAdvice | ||
public class CustomGlobalExceptionHandler extends ResponseEntityExceptionHandler { | ||
@Override | ||
protected ResponseEntity<Object> handleMethodArgumentNotValid( | ||
MethodArgumentNotValidException ex, | ||
@NonNull HttpHeaders headers, | ||
@NonNull HttpStatusCode status, | ||
@NonNull WebRequest request) { | ||
Map<String, Object> errors = new LinkedHashMap<>(); | ||
errors.put("timestamp", LocalDateTime.now()); | ||
errors.put("status", HttpStatus.BAD_REQUEST); | ||
List<String> errorMessages = ex.getBindingResult().getAllErrors().stream() | ||
.map(this::getErrorMessage) | ||
.toList(); | ||
errors.put("errors", errorMessages); | ||
return new ResponseEntity<>(errors, headers, HttpStatus.BAD_REQUEST); | ||
} | ||
|
||
private String getErrorMessage(ObjectError e) { | ||
if (e instanceof FieldError fieldError) { | ||
return fieldError.getField() + ": " + getMessage(e); | ||
} | ||
return getMessage(e); | ||
} | ||
|
||
private String getMessage(ObjectError e) { | ||
return e.getDefaultMessage() != null | ||
? e.getDefaultMessage() | ||
: "Something went wrong, please check your input"; | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
src/main/java/mate/academy/rickandmorty/exception/EntityNotFoundException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package mate.academy.rickandmorty.exception; | ||
|
||
public class EntityNotFoundException extends RuntimeException { | ||
public EntityNotFoundException(String message) { | ||
super(message); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
src/main/java/mate/academy/rickandmorty/mapper/PersonageMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package mate.academy.rickandmorty.mapper; | ||
|
||
import mate.academy.rickandmorty.config.MapperConfig; | ||
import mate.academy.rickandmorty.dto.external.PersonageDataDto; | ||
import mate.academy.rickandmorty.dto.internal.CreatePersonageRequestDto; | ||
import mate.academy.rickandmorty.dto.internal.PersonageDto; | ||
import mate.academy.rickandmorty.model.Personage; | ||
import org.mapstruct.Mapper; | ||
import org.mapstruct.Mapping; | ||
import org.mapstruct.MappingTarget; | ||
|
||
@Mapper(config = MapperConfig.class) | ||
public interface PersonageMapper { | ||
PersonageDto toDto(Personage personage); | ||
|
||
Personage toModel(CreatePersonageRequestDto requestDto); | ||
|
||
@Mapping(target = "externalId", source = "id") | ||
Personage toModelFromMeteData(PersonageDataDto dataDto); | ||
|
||
Personage updatePersonageFromDto( | ||
CreatePersonageRequestDto requestDto, | ||
@MappingTarget Personage personage); | ||
} |
Oops, something went wrong.