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

Florin Pop - tennis courts challenge #180

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
18 changes: 18 additions & 0 deletions src/main/java/com/tenniscourts/guests/CreateGuestRequestDTO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.tenniscourts.guests;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import javax.validation.constraints.NotBlank;

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class CreateGuestRequestDTO {

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

import com.tenniscourts.config.BaseRestController;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import lombok.AllArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

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

private final GuestService guestService;

@PostMapping
@ApiOperation("Add guest")
@ApiResponses(value = {
@ApiResponse(code = 201, message = "Guest added successfully"),
@ApiResponse(code = 400, message = "Bad Request. Invalid data")
})
public ResponseEntity<Void> addGuest(@RequestBody CreateGuestRequestDTO createGuestRequestDTO) {
return ResponseEntity.created(locationByEntity(guestService.addGuest(createGuestRequestDTO).getId())).build();
}

@PutMapping
@ApiOperation("Update guest")
@ApiResponses(value = {
@ApiResponse(code = 200, message = "Guest successfully updated"),
@ApiResponse(code = 400, message = "Bad Request. Invalid data"),
@ApiResponse(code = 404, message = "Not Found. Guest not found")
})
public ResponseEntity<GuestDTO> updateGuest(@RequestBody GuestDTO guestDTO) {
guestService.findGuestById(guestDTO.getId());
return ResponseEntity.ok(guestService.updateGuest(guestDTO));
}

@DeleteMapping("/{id}")
@ApiOperation("Delete guest")
@ApiResponses(value = {
@ApiResponse(code = 200, message = "Guest successfully deleted"),
@ApiResponse(code = 400, message = "Bad Request. Invalid id"),
@ApiResponse(code = 404, message = "Not Found. Guest not found")
})
public ResponseEntity<Void> deleteGuest(@PathVariable("id") Long guestId) {
guestService.findGuestById(guestId);
guestService.deleteGuest(guestId);
return ResponseEntity.ok().build();
}

@GetMapping("/{id}")
@ApiOperation("Find guest by Id")
@ApiResponses(value = {
@ApiResponse(code = 200, message = "Ok. Guest successfully found"),
@ApiResponse(code = 400, message = "Bad Request. Invalid Id"),
@ApiResponse(code = 404, message = "Not Found. Guest not found")
})
public ResponseEntity<GuestDTO> findGuestById(@PathVariable("id") Long guestId) {
return ResponseEntity.ok(guestService.findGuestById(guestId));
}

@GetMapping("/by-name")
@ApiOperation("Find guests containing name")
@ApiResponses(value = {
@ApiResponse(code = 200, message = "Ok. Guests successfully returned"),
@ApiResponse(code = 404, message = "Not Found. Guests not found")
})
public ResponseEntity<List<GuestDTO>> findGuestsContainingName(@RequestParam("name") String name) {
return ResponseEntity.ok(guestService.findGuestsContainingName(name));
}

@GetMapping
@ApiOperation("Find all guests")
@ApiResponses(value = {
@ApiResponse(code = 200, message = "Ok. All guests successfully returned"),
@ApiResponse(code = 404, message = "Not Found. Guests not found")
})
public ResponseEntity<List<GuestDTO>> findAllGuests() {
return ResponseEntity.ok(guestService.findAllGuests());
}
}
19 changes: 19 additions & 0 deletions src/main/java/com/tenniscourts/guests/GuestDTO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.tenniscourts.guests;

import lombok.*;

import javax.validation.constraints.NotBlank;

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

private Long id;

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

import org.mapstruct.InheritInverseConfiguration;
import org.mapstruct.Mapper;

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

GuestDTO map(Guest source);

@InheritInverseConfiguration
Guest map(GuestDTO source);

Guest map(CreateGuestRequestDTO 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 org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;

public interface GuestRepository extends JpaRepository<Guest, Long> {

List<Guest> findByNameContainingIgnoreCase(String name);
}
43 changes: 43 additions & 0 deletions src/main/java/com/tenniscourts/guests/GuestService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.tenniscourts.guests;

import com.tenniscourts.exceptions.EntityNotFoundException;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
@AllArgsConstructor
public class GuestService {

private final GuestRepository guestRepository;

private final GuestMapper guestMapper;

public GuestDTO findGuestById(Long id) {
return guestRepository.findById(id).map(guestMapper::map).orElseThrow(() -> {
throw new EntityNotFoundException("Guest not found.");
});
}

public List<GuestDTO> findGuestsContainingName(String name) {
return guestRepository.findByNameContainingIgnoreCase(name).stream().map(guestMapper::map).collect(Collectors.toList());
}

public List<GuestDTO> findAllGuests() {
return guestRepository.findAll().stream().map(guestMapper::map).collect(Collectors.toList());
}

public GuestDTO addGuest(CreateGuestRequestDTO createGuestRequestDTO) {
return guestMapper.map(guestRepository.saveAndFlush(guestMapper.map(createGuestRequestDTO)));
}

public GuestDTO updateGuest(GuestDTO guestDTO) {
return guestMapper.map(guestRepository.saveAndFlush(guestMapper.map(guestDTO)));
}

public void deleteGuest(Long id) {
guestRepository.deleteById(id);
}
}
Original file line number Diff line number Diff line change
@@ -1,27 +1,69 @@
package com.tenniscourts.reservations;

import com.tenniscourts.config.BaseRestController;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import lombok.AllArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("reservations")
@AllArgsConstructor
public class ReservationController extends BaseRestController {

private final ReservationService reservationService;

public ResponseEntity<Void> bookReservation(CreateReservationRequestDTO createReservationRequestDTO) {
@PostMapping
@ApiOperation("Book reservation")
@ApiResponses(value = {
@ApiResponse(code = 201, message = "Reservation successfully booked"),
@ApiResponse(code = 400, message = "Bad Request. Invalid data")
})
public ResponseEntity<Void> bookReservation(@RequestBody CreateReservationRequestDTO createReservationRequestDTO) {
return ResponseEntity.created(locationByEntity(reservationService.bookReservation(createReservationRequestDTO).getId())).build();
}

public ResponseEntity<ReservationDTO> findReservation(Long reservationId) {
@GetMapping("/{id}")
@ApiOperation("Find reservation by Id")
@ApiResponses(value = {
@ApiResponse(code = 200, message = "Ok. Reservation successfully found"),
@ApiResponse(code = 400, message = "Bad Request. Invalid Id"),
@ApiResponse(code = 404, message = "Not Found. Reservation not found")
})
public ResponseEntity<ReservationDTO> findReservation(@PathVariable("id") Long reservationId) {
return ResponseEntity.ok(reservationService.findReservation(reservationId));
}

public ResponseEntity<ReservationDTO> cancelReservation(Long reservationId) {
@PutMapping("/cancel/{id}")
@ApiOperation("Cancel reservation by Id")
@ApiResponses(value = {
@ApiResponse(code = 200, message = "Ok. Reservation successfully cancelled"),
@ApiResponse(code = 400, message = "Bad Request. Invalid Id"),
@ApiResponse(code = 404, message = "Not Found. Reservation not found")
})
public ResponseEntity<ReservationDTO> cancelReservation(@PathVariable("id") Long reservationId) {
return ResponseEntity.ok(reservationService.cancelReservation(reservationId));
}

public ResponseEntity<ReservationDTO> rescheduleReservation(Long reservationId, Long scheduleId) {
@PutMapping("/reschedule")
@ApiOperation("Reschedule reservation")
@ApiResponses(value = {
@ApiResponse(code = 200, message = "Reservation successfully rescheduled"),
@ApiResponse(code = 400, message = "Bad Request. Invalid data"),
@ApiResponse(code = 404, message = "Not Found. Reservation not found")
})
public ResponseEntity<ReservationDTO> rescheduleReservation(@RequestParam("reservationId") Long reservationId, @RequestParam("scheduleId") Long scheduleId) {
return ResponseEntity.ok(reservationService.rescheduleReservation(reservationId, scheduleId));
}

@GetMapping("/past-reservations")
@ApiOperation("Find all past reservations")
public ResponseEntity<List<ReservationDTO>> findAllPastReservations() {
return ResponseEntity.ok(reservationService.findAllPastReservations());
}

}
19 changes: 5 additions & 14 deletions src/main/java/com/tenniscourts/reservations/ReservationDTO.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
package com.tenniscourts.reservations;

import com.tenniscourts.guests.GuestDTO;
import com.tenniscourts.schedules.ScheduleDTO;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import javax.validation.constraints.NotNull;
import lombok.*;

import java.math.BigDecimal;

@AllArgsConstructor
Expand All @@ -21,6 +16,8 @@ public class ReservationDTO {

private Long id;

private GuestDTO guest;

private ScheduleDTO schedule;

private String reservationStatus;
Expand All @@ -30,10 +27,4 @@ public class ReservationDTO {
private BigDecimal refundValue;

private BigDecimal value;

@NotNull
private Long scheduledId;

@NotNull
private Long guestId;
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ public interface ReservationRepository extends JpaRepository<Reservation, Long>

List<Reservation> findBySchedule_Id(Long scheduleId);

List<Reservation> findBySchedule_EndDateTimeLessThan(LocalDateTime localDateTime);

List<Reservation> findByReservationStatusAndSchedule_StartDateTimeGreaterThanEqualAndSchedule_EndDateTimeLessThanEqual(ReservationStatus reservationStatus, LocalDateTime startDateTime, LocalDateTime endDateTime);

// List<Reservation> findByStartDateTimeGreaterThanEqualAndEndDateTimeLessThanEqualAndTennisCourt(LocalDateTime startDateTime, LocalDateTime endDateTime, TennisCourt tennisCourt);
Expand Down
Loading