-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
submit java-spring-ru/entity-validation
- Loading branch information
1 parent
22065ac
commit d696fec
Showing
5 changed files
with
144 additions
and
144 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
{"assignment":"java-spring-ru/dto-mapper"} | ||
{"assignment":"java-spring-ru/entity-validation"} |
Empty file.
114 changes: 57 additions & 57 deletions
114
java-spring-ru/entity-validation/src/main/java/exercise/controller/GuestsController.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 |
---|---|---|
@@ -1,57 +1,57 @@ | ||
package exercise.controller; | ||
|
||
import exercise.mapper.GuestMapper; | ||
import jakarta.validation.Valid; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.web.bind.annotation.RestController; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.web.bind.annotation.ResponseStatus; | ||
import java.util.List; | ||
|
||
import exercise.repository.GuestRepository; | ||
import exercise.dto.GuestDTO; | ||
import exercise.dto.GuestCreateDTO; | ||
import exercise.exception.ResourceNotFoundException; | ||
|
||
@RestController | ||
@RequestMapping("/guests") | ||
public class GuestsController { | ||
|
||
@Autowired | ||
private GuestRepository guestRepository; | ||
|
||
@Autowired | ||
private GuestMapper guestMapper; | ||
|
||
@GetMapping(path = "") | ||
public List<GuestDTO> index() { | ||
var products = guestRepository.findAll(); | ||
return products.stream() | ||
.map(p -> guestMapper.map(p)) | ||
.toList(); | ||
} | ||
|
||
@GetMapping(path = "/{id}") | ||
public GuestDTO show(@PathVariable long id) { | ||
|
||
var guest = guestRepository.findById(id) | ||
.orElseThrow(() -> new ResourceNotFoundException("Guest with id " + id + " not found")); | ||
var guestDto = guestMapper.map(guest); | ||
return guestDto; | ||
} | ||
|
||
// BEGIN | ||
@PostMapping("") | ||
@ResponseStatus(HttpStatus.CREATED) | ||
public GuestDTO create(@Valid @RequestBody GuestCreateDTO newGuest) { | ||
var guest = guestMapper.map(newGuest); | ||
guestRepository.save(guest); | ||
return guestMapper.map(guest); | ||
} | ||
// END | ||
} | ||
package exercise.controller; | ||
|
||
import exercise.mapper.GuestMapper; | ||
import jakarta.validation.Valid; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.web.bind.annotation.RestController; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.web.bind.annotation.ResponseStatus; | ||
import java.util.List; | ||
|
||
import exercise.repository.GuestRepository; | ||
import exercise.dto.GuestDTO; | ||
import exercise.dto.GuestCreateDTO; | ||
import exercise.exception.ResourceNotFoundException; | ||
|
||
@RestController | ||
@RequestMapping("/guests") | ||
public class GuestsController { | ||
|
||
@Autowired | ||
private GuestRepository guestRepository; | ||
|
||
@Autowired | ||
private GuestMapper guestMapper; | ||
|
||
@GetMapping(path = "") | ||
public List<GuestDTO> index() { | ||
var products = guestRepository.findAll(); | ||
return products.stream() | ||
.map(p -> guestMapper.map(p)) | ||
.toList(); | ||
} | ||
|
||
@GetMapping(path = "/{id}") | ||
public GuestDTO show(@PathVariable long id) { | ||
|
||
var guest = guestRepository.findById(id) | ||
.orElseThrow(() -> new ResourceNotFoundException("Guest with id " + id + " not found")); | ||
var guestDto = guestMapper.map(guest); | ||
return guestDto; | ||
} | ||
|
||
// BEGIN | ||
@PostMapping("") | ||
@ResponseStatus(HttpStatus.CREATED) | ||
public GuestDTO create(@Valid @RequestBody GuestCreateDTO newGuest) { | ||
var guest = guestMapper.map(newGuest); | ||
guestRepository.save(guest); | ||
return guestMapper.map(guest); | ||
} | ||
// END | ||
} |
74 changes: 37 additions & 37 deletions
74
java-spring-ru/entity-validation/src/main/java/exercise/dto/GuestCreateDTO.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 |
---|---|---|
@@ -1,37 +1,37 @@ | ||
package exercise.dto; | ||
|
||
// BEGIN | ||
|
||
import jakarta.persistence.GeneratedValue; | ||
import jakarta.persistence.Id; | ||
import jakarta.validation.constraints.Email; | ||
import jakarta.validation.constraints.FutureOrPresent; | ||
import jakarta.validation.constraints.NotNull; | ||
import jakarta.validation.constraints.Pattern; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
import org.springframework.data.annotation.CreatedDate; | ||
|
||
import java.time.LocalDate; | ||
|
||
import static jakarta.persistence.GenerationType.IDENTITY; | ||
|
||
@Getter | ||
@Setter | ||
public class GuestCreateDTO { | ||
@NotNull | ||
private String name; | ||
|
||
private String email; | ||
|
||
@Pattern(regexp = "\\+\\d{11,13}") | ||
private String phoneNumber; | ||
|
||
@Pattern(regexp = "\\d{4}") | ||
private String clubCard; | ||
|
||
@FutureOrPresent | ||
private LocalDate cardValidUntil; | ||
} | ||
// END | ||
package exercise.dto; | ||
|
||
// BEGIN | ||
|
||
import jakarta.persistence.GeneratedValue; | ||
import jakarta.persistence.Id; | ||
import jakarta.validation.constraints.Email; | ||
import jakarta.validation.constraints.FutureOrPresent; | ||
import jakarta.validation.constraints.NotNull; | ||
import jakarta.validation.constraints.Pattern; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
import org.springframework.data.annotation.CreatedDate; | ||
|
||
import java.time.LocalDate; | ||
|
||
import static jakarta.persistence.GenerationType.IDENTITY; | ||
|
||
@Getter | ||
@Setter | ||
public class GuestCreateDTO { | ||
@NotNull | ||
private String name; | ||
|
||
private String email; | ||
|
||
@Pattern(regexp = "\\+\\d{11,13}") | ||
private String phoneNumber; | ||
|
||
@Pattern(regexp = "\\d{4}") | ||
private String clubCard; | ||
|
||
@FutureOrPresent | ||
private LocalDate cardValidUntil; | ||
} | ||
// END |
98 changes: 49 additions & 49 deletions
98
java-spring-ru/entity-validation/src/main/java/exercise/model/Guest.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 |
---|---|---|
@@ -1,49 +1,49 @@ | ||
package exercise.model; | ||
|
||
import jakarta.persistence.Column; | ||
import jakarta.persistence.Id; | ||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.Table; | ||
import jakarta.persistence.EntityListeners; | ||
import jakarta.persistence.GeneratedValue; | ||
import static jakarta.persistence.GenerationType.IDENTITY; | ||
|
||
import jakarta.validation.constraints.*; | ||
import org.springframework.data.annotation.CreatedDate; | ||
import org.springframework.data.jpa.domain.support.AuditingEntityListener; | ||
|
||
import lombok.Getter; | ||
import lombok.Setter; | ||
import java.time.LocalDate; | ||
|
||
@Entity | ||
@Table(name = "guests") | ||
@EntityListeners(AuditingEntityListener.class) | ||
@Getter | ||
@Setter | ||
public class Guest { | ||
|
||
@Id | ||
@GeneratedValue(strategy = IDENTITY) | ||
private long id; | ||
|
||
// BEGIN | ||
@NotNull | ||
private String name; | ||
|
||
private String email; | ||
|
||
@Pattern(regexp = "^\\+\\d{11,13}$") | ||
private String phoneNumber; | ||
|
||
@Pattern(regexp = "^\\d{4}$") | ||
private String clubCard; | ||
|
||
@FutureOrPresent | ||
private LocalDate cardValidUntil; | ||
// END | ||
|
||
@CreatedDate | ||
private LocalDate createdAt; | ||
} | ||
package exercise.model; | ||
|
||
import jakarta.persistence.Column; | ||
import jakarta.persistence.Id; | ||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.Table; | ||
import jakarta.persistence.EntityListeners; | ||
import jakarta.persistence.GeneratedValue; | ||
import static jakarta.persistence.GenerationType.IDENTITY; | ||
|
||
import jakarta.validation.constraints.*; | ||
import org.springframework.data.annotation.CreatedDate; | ||
import org.springframework.data.jpa.domain.support.AuditingEntityListener; | ||
|
||
import lombok.Getter; | ||
import lombok.Setter; | ||
import java.time.LocalDate; | ||
|
||
@Entity | ||
@Table(name = "guests") | ||
@EntityListeners(AuditingEntityListener.class) | ||
@Getter | ||
@Setter | ||
public class Guest { | ||
|
||
@Id | ||
@GeneratedValue(strategy = IDENTITY) | ||
private long id; | ||
|
||
// BEGIN | ||
@NotNull | ||
private String name; | ||
|
||
private String email; | ||
|
||
@Pattern(regexp = "^\\+\\d{11,13}$") | ||
private String phoneNumber; | ||
|
||
@Pattern(regexp = "^\\d{4}$") | ||
private String clubCard; | ||
|
||
@FutureOrPresent | ||
private LocalDate cardValidUntil; | ||
// END | ||
|
||
@CreatedDate | ||
private LocalDate createdAt; | ||
} |