Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implementation of required user stories #172

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,15 @@ dependencies {
testCompile('org.springframework.boot:spring-boot-starter-test')
testCompile('org.springframework.restdocs:spring-restdocs-mockmvc')

testImplementation 'io.rest-assured:rest-assured:4.2.1'
testImplementation 'io.rest-assured:json-path:4.2.1'
testImplementation 'io.rest-assured:xml-path:4.2.1'
testImplementation 'io.rest-assured:json-schema-validator:4.2.1'

compile group: 'com.h2database', name: 'h2', version: '1.4.197'
compile group: 'io.springfox', name: 'springfox-swagger2', version: '2.9.2'
compile group: 'io.springfox', name: 'springfox-swagger-ui', version: '2.9.2'
compile group: 'org.unitils', name: 'unitils-core', version: '3.3'
implementation group: 'org.mapstruct', name: 'mapstruct-jdk8', version: '1.2.0.Final'
annotationProcessor 'org.mapstruct:mapstruct-processor:1.2.0.Final'
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package com.tenniscourts.audit;

import com.tenniscourts.config.persistence.BaseEntity;

import javax.persistence.PrePersist;
import javax.persistence.PreUpdate;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.time.LocalDateTime;
import java.util.Objects;
import javax.persistence.PrePersist;
import javax.persistence.PreUpdate;

public class CustomAuditEntityListener {

Expand Down
5 changes: 2 additions & 3 deletions src/main/java/com/tenniscourts/config/BaseRestController.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package com.tenniscourts.config;

import java.net.URI;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
import springfox.documentation.annotations.ApiIgnore;

import java.net.URI;

@ApiIgnore
public class BaseRestController {

protected URI locationByEntity(Long entityId){
protected URI locationByEntity(Long entityId) {
return ServletUriComponentsBuilder.fromCurrentRequest().path(
"/{id}").buildAndExpand(entityId).toUri();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
package com.tenniscourts.config.persistence;

import com.tenniscourts.audit.CustomAuditEntityListener;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;

import java.io.Serializable;
import java.time.LocalDateTime;
import javax.persistence.Column;
import javax.persistence.EntityListeners;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;
import java.io.Serializable;
import java.time.LocalDateTime;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;

@MappedSuperclass
@Getter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@
* The type Already exists entity exception.
*/
public class AlreadyExistsEntityException extends RuntimeException {
/**
* Instantiates a new Already exists entity exception.
*
* @param msg the msg
*/
public AlreadyExistsEntityException(String msg){

/**
* Instantiates a new Already exists entity exception.
*
* @param msg the msg
*/
public AlreadyExistsEntityException(String msg) {
super(msg);
}

private AlreadyExistsEntityException(){}
private AlreadyExistsEntityException() {
}
}
16 changes: 9 additions & 7 deletions src/main/java/com/tenniscourts/exceptions/BusinessException.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@
* The type Business exception.
*/
public class BusinessException extends RuntimeException {
/**
* Instantiates a new Business exception.
*
* @param msg the msg
*/
public BusinessException(String msg){

/**
* Instantiates a new Business exception.
*
* @param msg the msg
*/
public BusinessException(String msg) {
super(msg);
}

private BusinessException(){}
private BusinessException() {
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package com.tenniscourts.exceptions;

import static org.springframework.http.HttpStatus.BAD_REQUEST;
import static org.springframework.http.HttpStatus.UNSUPPORTED_MEDIA_TYPE;

import java.time.LocalDateTime;
import java.util.stream.Collectors;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.http.HttpHeaders;
Expand All @@ -12,19 +17,14 @@
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;

import java.time.LocalDateTime;
import java.util.stream.Collectors;

import static org.springframework.http.HttpStatus.BAD_REQUEST;
import static org.springframework.http.HttpStatus.UNSUPPORTED_MEDIA_TYPE;


@Order(Ordered.HIGHEST_PRECEDENCE)
@ControllerAdvice
public class CustomResponseEntityExceptionHandler extends ResponseEntityExceptionHandler {

@ExceptionHandler(AlreadyExistsEntityException.class)
public final ResponseEntity<ErrorDetails> handleEntityAlreadyExists(AlreadyExistsEntityException ex, WebRequest request) {
public final ResponseEntity<ErrorDetails> handleEntityAlreadyExists(AlreadyExistsEntityException ex,
WebRequest request) {
ErrorDetails errorDetails = new ErrorDetails(LocalDateTime.now(), ex.getMessage(),
request.getDescription(false));
return new ResponseEntity<>(errorDetails, HttpStatus.CONFLICT);
Expand All @@ -45,7 +45,8 @@ public final ResponseEntity<ErrorDetails> handleBusinessException(EntityNotFound
}

@ExceptionHandler(IllegalArgumentException.class)
public final ResponseEntity<ErrorDetails> handleIllegalArgumentException(IllegalArgumentException ex, WebRequest request) {
public final ResponseEntity<ErrorDetails> handleIllegalArgumentException(IllegalArgumentException ex,
WebRequest request) {
ErrorDetails errorDetails = new ErrorDetails(LocalDateTime.now(), ex.getMessage(),
request.getDescription(false));
return new ResponseEntity<>(errorDetails, BAD_REQUEST);
Expand All @@ -69,7 +70,8 @@ protected ResponseEntity<Object> handleHttpMediaTypeNotSupported(
builder.append(ex.getContentType());
builder.append(" media type is not supported. Supported media types are ");
ex.getSupportedMediaTypes().forEach(t -> builder.append(t).append(", "));
return buildResponseEntity(new ErrorDetails(LocalDateTime.now(), builder.substring(0, builder.length() - 2), ex.getLocalizedMessage()), UNSUPPORTED_MEDIA_TYPE);
return buildResponseEntity(new ErrorDetails(LocalDateTime.now(), builder.substring(0, builder.length() - 2),
ex.getLocalizedMessage()), UNSUPPORTED_MEDIA_TYPE);
}

@ExceptionHandler(javax.validation.ConstraintViolationException.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@
* The type Entity not found exception.
*/
public class EntityNotFoundException extends RuntimeException {
/**
* Instantiates a new Entity not found exception.
*
* @param msg the msg
*/
public EntityNotFoundException(String msg){

/**
* Instantiates a new Entity not found exception.
*
* @param msg the msg
*/
public EntityNotFoundException(String msg) {
super(msg);
}

private EntityNotFoundException(){}
private EntityNotFoundException() {
}
}
3 changes: 1 addition & 2 deletions src/main/java/com/tenniscourts/exceptions/ErrorDetails.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package com.tenniscourts.exceptions;

import java.time.LocalDateTime;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.time.LocalDateTime;

@NoArgsConstructor
@AllArgsConstructor
@Getter
Expand Down
15 changes: 7 additions & 8 deletions src/main/java/com/tenniscourts/guests/Guest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package com.tenniscourts.guests;

import com.tenniscourts.config.persistence.BaseEntity;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.EqualsAndHashCode;
Expand All @@ -9,11 +13,6 @@
import lombok.Setter;
import lombok.ToString;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;

@Entity
@Table
@Getter
Expand All @@ -25,8 +24,8 @@
@ToString
public class Guest extends BaseEntity<Long> {

@Column
@NotNull
private String name;
@Column
@NotNull
private String name;

}
61 changes: 61 additions & 0 deletions src/main/java/com/tenniscourts/guests/GuestController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package com.tenniscourts.guests;

import com.tenniscourts.config.BaseRestController;
import io.swagger.annotations.ApiOperation;
import java.util.List;
import lombok.AllArgsConstructor;
import org.springframework.http.ResponseEntity;
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.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@AllArgsConstructor
@RequestMapping("/guests")
public class GuestController extends BaseRestController {

private final GuestService guestService;

@GetMapping
@ApiOperation(value = "List all guests")
public ResponseEntity<List<GuestDTO>> findAllGuests() {
return ResponseEntity.ok(guestService.findAll());
}

@GetMapping("/{guestId}")
@ApiOperation(value = "Find guest by id")
public ResponseEntity<GuestDTO> findGuestById(@PathVariable Long guestId) {
return ResponseEntity.ok(guestService.findById(guestId));
}

@GetMapping("/guest")
@ApiOperation(value = "Find guest by name")
public ResponseEntity<GuestDTO> findGuestByName(@RequestParam(value = "name") String guestName) {
return ResponseEntity.ok(guestService.findByName(guestName));
}

@PostMapping
@ApiOperation(value = "Add a guest")
public ResponseEntity<GuestDTO> addGuest(@RequestBody GuestDTO guest) {
return ResponseEntity.ok(guestService.add(guest));
}

@PutMapping
@ApiOperation(value = "Update a guest")
public ResponseEntity<GuestDTO> updateGuest(@RequestBody GuestDTO guest) {
return ResponseEntity.ok(guestService.update(guest));
}

@DeleteMapping("/{guestId}")
@ApiOperation(value = "Delete a guest by id")
public ResponseEntity<Void> deleteGuest(@PathVariable Long guestId) {
guestService.delete(guestId);
return ResponseEntity.ok().build();
}
}
21 changes: 21 additions & 0 deletions src/main/java/com/tenniscourts/guests/GuestDTO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.tenniscourts.guests;

import javax.validation.constraints.NotNull;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@AllArgsConstructor
@NoArgsConstructor
@Builder
@Getter
@Setter
public class GuestDTO {

private Long id;

@NotNull
private String name;
}
17 changes: 17 additions & 0 deletions src/main/java/com/tenniscourts/guests/GuestMapper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.tenniscourts.guests;

import java.util.List;
import org.mapstruct.InheritInverseConfiguration;
import org.mapstruct.Mapper;

@Mapper(componentModel = "spring")
public interface GuestMapper {

Guest map(GuestDTO source);

@InheritInverseConfiguration
GuestDTO map(Guest source);

List<GuestDTO> map(List<Guest> source);

}
10 changes: 10 additions & 0 deletions src/main/java/com/tenniscourts/guests/GuestRepository.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.tenniscourts.guests;

import java.util.Optional;
import org.springframework.data.jpa.repository.JpaRepository;

public interface GuestRepository extends JpaRepository<Guest, Long> {

Optional<Guest> findByName(String name);

}
Loading