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

solution to 1-7 #176

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
17 changes: 17 additions & 0 deletions src/main/java/com/tenniscourts/guests/CreateGuestRequestDTO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.tenniscourts.guests;

import lombok.*;

import javax.validation.constraints.NotNull;

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

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

import lombok.*;

import javax.validation.constraints.NotNull;

@AllArgsConstructor
@NoArgsConstructor
@Getter
@Setter
@Builder
@Data
public class CreateSearchRequestDTO {

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

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

import javax.validation.constraints.NotNull;
import java.util.List;

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

private final GuestService guestService;

@ApiModelProperty(value = "Create a guest")
@PostMapping
public ResponseEntity<Void> createGuest(@RequestBody CreateGuestRequestDTO createGuestRequestDTO) {
return ResponseEntity.created(locationByEntity(guestService.createGuest(createGuestRequestDTO).getId())).build();
}

@ApiModelProperty(value = "Update a guest")
@PatchMapping(value = "/{id}")
public ResponseEntity<GuestDTO> updateGuest(@PathVariable Long id, @RequestBody UpdateGuestRequestDTO updateGuestRequestDTO) {
return ResponseEntity.ok(guestService.updateGuest(id, updateGuestRequestDTO));
}

@ApiModelProperty(value = "Delete a guest")
@DeleteMapping(value = "/{id}")
public ResponseEntity<Void> deleteGuest(@PathVariable Long id) {
guestService.deleteGuest(id);
return ResponseEntity.noContent().build();
}

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

@ApiModelProperty(value = "Find guests by name")
@GetMapping(value = "/filter")
public ResponseEntity<GuestDTO> findGuestsByName(@RequestParam("name") @NotNull String name) {
return ResponseEntity.ok(guestService.getByName(name));
}

@ApiModelProperty(value = "Find all guests")
@GetMapping
public ResponseEntity<List<GuestDTO>> getAll() {
return ResponseEntity.ok(guestService.getAll());
}

}
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.NotNull;

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

private Long id;

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

import org.mapstruct.Mapper;

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

Guest map(GuestDTO guestDTO);

GuestDTO map(Guest guest);

Guest map(CreateGuestRequestDTO createGuestRequestDTO);
}
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.Optional;

public interface GuestRepository extends JpaRepository<Guest, Long> {

Optional<Guest> findByName(String name);
}
54 changes: 54 additions & 0 deletions src/main/java/com/tenniscourts/guests/GuestService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
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 createGuest(CreateGuestRequestDTO createGuestRequestDTO) {
return guestMapper.map(guestRepository.save(guestMapper.map(createGuestRequestDTO)));
}

public GuestDTO updateGuest(Long id, UpdateGuestRequestDTO updateGuestRequestDTO) {
Guest existingGuest = guestRepository.findById(id).orElseThrow(() -> {
throw new EntityNotFoundException("Guest not found.");
});
existingGuest.setName(updateGuestRequestDTO.getName());

return guestMapper.map(guestRepository.save(existingGuest));
}

public void deleteGuest(Long id) {
guestRepository.findById(id).ifPresentOrElse(
(guest) -> guestRepository.deleteById(guest.getId()),
() -> {
throw new EntityNotFoundException("Guest not found.");
});
}

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

public GuestDTO getByName(String name) {
return guestRepository.findByName(name).map(guestMapper::map).orElseThrow(()->{
throw new EntityNotFoundException("Guest not found.");
});
}

public List<GuestDTO> getAll() {
return guestRepository.findAll().stream().map(guestMapper::map).collect(Collectors.toList());
}
}
17 changes: 17 additions & 0 deletions src/main/java/com/tenniscourts/guests/UpdateGuestRequestDTO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.tenniscourts.guests;

import lombok.*;

import javax.validation.constraints.NotNull;

@AllArgsConstructor
@NoArgsConstructor
@Getter
@Setter
@Builder
@Data
public class UpdateGuestRequestDTO {

@NotNull
private String name;
}
Original file line number Diff line number Diff line change
@@ -1,27 +1,39 @@
package com.tenniscourts.reservations;

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

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

private final ReservationService reservationService;

public ResponseEntity<Void> bookReservation(CreateReservationRequestDTO createReservationRequestDTO) {
@ApiModelProperty(value = "Book a reservation")
@PostMapping
public ResponseEntity<Void> bookReservation(@RequestBody CreateReservationRequestDTO createReservationRequestDTO) {
return ResponseEntity.created(locationByEntity(reservationService.bookReservation(createReservationRequestDTO).getId())).build();
}

public ResponseEntity<ReservationDTO> findReservation(Long reservationId) {
@ApiModelProperty(value = "Find a reservation by id")
@GetMapping(value = "/{reservationId}")
public ResponseEntity<ReservationDTO> findReservation(@PathVariable("reservationId") Long reservationId) {
return ResponseEntity.ok(reservationService.findReservation(reservationId));
}

public ResponseEntity<ReservationDTO> cancelReservation(Long reservationId) {
@ApiModelProperty(value = "Cancel reservation")
@DeleteMapping(value = "/{reservationId}")
public ResponseEntity<ReservationDTO> cancelReservation(@PathVariable Long reservationId) {
return ResponseEntity.ok(reservationService.cancelReservation(reservationId));
}

public ResponseEntity<ReservationDTO> rescheduleReservation(Long reservationId, Long scheduleId) {
@ApiModelProperty(value = "Reschedule reservation")
@PutMapping(value = "/{reservationId}/{scheduleId}")
public ResponseEntity<ReservationDTO> rescheduleReservation(@PathVariable Long reservationId, @PathVariable Long scheduleId) {
return ResponseEntity.ok(reservationService.rescheduleReservation(reservationId, scheduleId));
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
package com.tenniscourts.reservations;

import com.tenniscourts.exceptions.AlreadyExistsEntityException;
import com.tenniscourts.exceptions.EntityNotFoundException;
import com.tenniscourts.guests.Guest;
import com.tenniscourts.guests.GuestRepository;
import com.tenniscourts.schedules.Schedule;
import com.tenniscourts.schedules.ScheduleRepository;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;

@Service
@AllArgsConstructor
Expand All @@ -16,8 +22,33 @@ public class ReservationService {

private final ReservationMapper reservationMapper;

private final ScheduleRepository scheduleRepository;

private final GuestRepository guestRepository;

public ReservationDTO bookReservation(CreateReservationRequestDTO createReservationRequestDTO) {
throw new UnsupportedOperationException();
Long guestId = createReservationRequestDTO.getGuestId();
Long scheduleId = createReservationRequestDTO.getScheduleId();

Guest guest = guestRepository.findById(guestId).orElseThrow(() -> {
throw new EntityNotFoundException("Guest does not exist.");
});
Schedule schedule = scheduleRepository.findById(scheduleId).orElseThrow(() -> {
throw new EntityNotFoundException("Schedule does not exist.");
});
if (!schedule.getReservations().isEmpty()) {
throw new AlreadyExistsEntityException("This time slot is already booked.");
}

Reservation reservation = Reservation.builder()
.guest(guest)
.schedule(schedule)
.value(BigDecimal.TEN)
.reservationStatus(ReservationStatus.READY_TO_PLAY)
.build();
schedule.addReservation(reservation);

return reservationMapper.map(reservationRepository.save(reservation));
}

public ReservationDTO findReservation(Long reservationId) {
Expand Down Expand Up @@ -47,6 +78,9 @@ private Reservation updateReservation(Reservation reservation, BigDecimal refund
reservation.setReservationStatus(status);
reservation.setValue(reservation.getValue().subtract(refundValue));
reservation.setRefundValue(refundValue);
Schedule schedule = reservation.getSchedule();
schedule.setReservations(new ArrayList<>());
reservation.setSchedule(schedule);

return reservationRepository.save(reservation);
}
Expand All @@ -62,24 +96,24 @@ private void validateCancellation(Reservation reservation) {
}

public BigDecimal getRefundValue(Reservation reservation) {
long hours = ChronoUnit.HOURS.between(LocalDateTime.now(), reservation.getSchedule().getStartDateTime());
long minutes = ChronoUnit.MINUTES.between(LocalDateTime.now(), reservation.getSchedule().getStartDateTime());

if (hours >= 24) {
if (minutes >= 1440) {
return reservation.getValue();
} else if (minutes >= 720) {
return reservation.getValue().multiply(BigDecimal.valueOf(0.75));
} else if (minutes >= 120) {
return reservation.getValue().multiply(BigDecimal.valueOf(0.5));
} else if (minutes >= 1) {
return reservation.getValue().multiply(BigDecimal.valueOf(0.25));
}

return BigDecimal.ZERO;
}

/*TODO: This method actually not fully working, find a way to fix the issue when it's throwing the error:
"Cannot reschedule to the same slot.*/
public ReservationDTO rescheduleReservation(Long previousReservationId, Long scheduleId) {
Reservation previousReservation = cancel(previousReservationId);

if (scheduleId.equals(previousReservation.getSchedule().getId())) {
throw new IllegalArgumentException("Cannot reschedule to the same slot.");
}

previousReservation.setReservationStatus(ReservationStatus.RESCHEDULED);
reservationRepository.save(previousReservation);

Expand Down
Loading