Skip to content

Commit

Permalink
submit java-spring-ru/entity-validation
Browse files Browse the repository at this point in the history
  • Loading branch information
hexlet-cli committed Feb 19, 2024
1 parent 22065ac commit d696fec
Show file tree
Hide file tree
Showing 5 changed files with 144 additions and 144 deletions.
2 changes: 1 addition & 1 deletion .current.json
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 modified java-spring-ru/entity-validation/gradlew
100644 → 100755
Empty file.
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
}
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;

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

@Email
private String email;

@Pattern(regexp = "\\+\\d{11,13}")
private String phoneNumber;

@Pattern(regexp = "\\d{4}")
private String clubCard;

@FutureOrPresent
private LocalDate cardValidUntil;
}
// END
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;

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

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

0 comments on commit d696fec

Please sign in to comment.