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

Points 1 to 8 of the readme #168

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Some fixes and tests
  • Loading branch information
fer-rouco committed Feb 22, 2022
commit 1f218800a88e9a37e71ce23fb3dadbf14c41fcdb
2 changes: 1 addition & 1 deletion src/main/java/com/tenniscourts/guests/GuestController.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public ResponseEntity<GuestDTO> findById(@PathVariable("id") Long id) {
}

@GetMapping()
@ApiOperation("Returns a list of guests searched by name. It searches with a part of the name or with the full name and ignore case.")
@ApiOperation("Returns a list of guests search by name, entering the full name or part of the name ignoring the case.")
public ResponseEntity<List<GuestDTO>> findByName(@RequestParam("name") String name) {
return ResponseEntity.ok(guestService.findByName(name));
}
Expand Down
6 changes: 2 additions & 4 deletions src/main/java/com/tenniscourts/guests/GuestService.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package com.tenniscourts.guests;

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

import com.tenniscourts.exceptions.BusinessException;
Expand All @@ -16,9 +14,9 @@
@AllArgsConstructor
public class GuestService {

private final GuestRepository guestRepository;
private GuestRepository guestRepository;

private final GuestMapper guestMapper;
private GuestMapper guestMapper;

public List<GuestDTO> findAll() {
return guestRepository.findAll().stream().map(guest -> { return guestMapper.map(guest); }).collect(Collectors.toList());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.tenniscourts.reservations;

import java.math.BigDecimal;
import java.util.List;

import com.tenniscourts.config.BaseRestController;
import lombok.AllArgsConstructor;
Expand Down Expand Up @@ -54,4 +55,16 @@ public ResponseEntity<ReservationDTO> rescheduleReservation(@PathVariable("id")
public ResponseEntity<ReservationDTO> chargeReservationDeposit(@PathVariable("id") Long id, @RequestParam(name = "deposit", required = false, defaultValue = "10") BigDecimal deposit) {
return ResponseEntity.ok(reservationService.chargeDeposit(id, deposit));
}

// @PutMapping("/keepDeposit/{id}")
// @ApiOperation("Keep a deposit from a specific reservation by their identifier. 404 if does not exist.")
// public ResponseEntity<ReservationDTO> keepReservationDeposit(@PathVariable("id") Long id, @RequestParam(name = "deposit", required = false, defaultValue = "10") BigDecimal deposit) {
// return ResponseEntity.ok(reservationService.keepDeposit(id));
// }

@GetMapping("/history")
@ApiOperation("Returns a list of past reservations.")
public ResponseEntity<List<ReservationDTO>> history() {
return ResponseEntity.ok(reservationService.getPastReservations());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,7 @@ public interface ReservationRepository extends JpaRepository<Reservation, Long>

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

List<Reservation> findBySchedule_StartDateTimeLessThanEqual(LocalDateTime startDateTime);

// List<Reservation> findByStartDateTimeGreaterThanEqualAndEndDateTimeLessThanEqualAndTennisCourt(LocalDateTime startDateTime, LocalDateTime endDateTime, TennisCourt tennisCourt);
}
53 changes: 37 additions & 16 deletions src/main/java/com/tenniscourts/reservations/ReservationService.java
Original file line number Diff line number Diff line change
@@ -1,32 +1,30 @@
package com.tenniscourts.reservations;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.List;
import java.util.stream.Collectors;

import com.tenniscourts.exceptions.EntityNotFoundException;
import com.tenniscourts.guests.GuestDTO;
import com.tenniscourts.guests.GuestService;
import com.tenniscourts.schedules.ScheduleDTO;
import com.tenniscourts.schedules.ScheduleService;

import lombok.AllArgsConstructor;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.Optional;
import lombok.AllArgsConstructor;

@Service
@AllArgsConstructor
public class ReservationService {

private final ReservationRepository reservationRepository;
private ReservationRepository reservationRepository;

private final ReservationMapper reservationMapper;
private ReservationMapperImpl reservationMapper;

private final GuestService guestService;
private GuestService guestService;

private final ScheduleService scheduleService;
private ScheduleService scheduleService;

public ReservationDTO bookReservation(CreateReservationRequestDTO createReservationRequestDTO) {
ReservationDTO reservationDTO = new ReservationDTO();
Expand All @@ -38,7 +36,7 @@ public ReservationDTO bookReservation(CreateReservationRequestDTO createReservat

public ReservationDTO findReservation(Long reservationId) {
return reservationRepository.findById(reservationId).map(reservationMapper::map).orElseThrow(() -> {
throw new EntityNotFoundException("Reservation not found.");
throw new EntityNotFoundException("Reservation " + reservationId + " not found.");
});
}

Expand All @@ -59,7 +57,7 @@ private Reservation cancel(Long reservationId) {
});
}

private Reservation updateReservation(Reservation reservation, BigDecimal refundValue, ReservationStatus status) {
private Reservation updateReservation(Reservation reservation, BigDecimal refundValue,ReservationStatus status) {
reservation.setReservationStatus(status);
reservation.setValue(reservation.getValue().subtract(refundValue));
reservation.setRefundValue(refundValue);
Expand All @@ -80,7 +78,20 @@ private void validateCancellation(Reservation reservation) {
public BigDecimal getRefundValue(Reservation reservation) {
long hours = ChronoUnit.HOURS.between(LocalDateTime.now(), reservation.getSchedule().getStartDateTime());

if (hours >= 24) {
if (hours < 2) {
// keep 75% and refund 25%
return BigDecimal.valueOf(reservation.getValue().doubleValue() * BigDecimal.valueOf(0.25).doubleValue());
}
else if (hours >= 2 && hours < 12) {
// keep 50% and refund 50%
return BigDecimal.valueOf(reservation.getValue().doubleValue() * BigDecimal.valueOf(0.50).doubleValue());
}
else if (hours >= 12 && hours < 24) {
// keep 25% and refund 75%
return BigDecimal.valueOf(reservation.getValue().doubleValue() * BigDecimal.valueOf(0.75).doubleValue());
}
else if (hours >= 24) {
// refund 100%
return reservation.getValue();
}

Expand Down Expand Up @@ -112,4 +123,14 @@ public ReservationDTO chargeDeposit(Long reservationId, BigDecimal deposit) {
reservationDTO.setValue(deposit);
return reservationMapper.map(reservationRepository.save(reservationMapper.map(reservationDTO)));
}

// public ReservationDTO keepDeposit(Long reservationId) {
// ReservationDTO reservationDTO = findReservation(reservationId);
// return reservationMapper.map(reservationRepository.save(reservationMapper.map(reservationDTO)));
// }

public List<ReservationDTO> getPastReservations() {
List<Reservation> historicReservations = reservationRepository.findBySchedule_StartDateTimeLessThanEqual(LocalDateTime.now());
return historicReservations.stream().map(historicReservation -> reservationMapper.map(historicReservation)).collect(Collectors.toList());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Builder;
import lombok.Getter;
import lombok.Setter;

Expand All @@ -12,6 +13,7 @@

@Getter
@Setter
@Builder
@ApiModel(description = "Class representing a request object to create a new schedule.")
public class CreateScheduleRequestDTO {

Expand Down
7 changes: 7 additions & 0 deletions src/main/java/com/tenniscourts/schedules/ScheduleDTO.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,21 @@
import io.swagger.annotations.ApiModelProperty;

import com.fasterxml.jackson.annotation.JsonFormat;

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

import javax.validation.constraints.NotNull;
import java.time.LocalDateTime;

@Getter
@Setter
@Builder
@NoArgsConstructor
@AllArgsConstructor
@ApiModel(description = "Class representing a schedule tracked by the application.")
public class ScheduleDTO {

Expand Down
33 changes: 17 additions & 16 deletions src/main/java/com/tenniscourts/schedules/ScheduleService.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
package com.tenniscourts.schedules;

import lombok.AllArgsConstructor;

import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;
Expand All @@ -12,26 +8,31 @@
import com.tenniscourts.tenniscourts.TennisCourtMapper;
import com.tenniscourts.tenniscourts.TennisCourtRepository;

import org.springframework.stereotype.Service;

import lombok.AllArgsConstructor;

@Service
@AllArgsConstructor
public class ScheduleService {

private final ScheduleRepository scheduleRepository;
private ScheduleRepository scheduleRepository;

private final ScheduleMapper scheduleMapper;
private ScheduleMapper scheduleMapper;

private final TennisCourtRepository tennisCourtRepository;
private TennisCourtRepository tennisCourtRepository;

private final TennisCourtMapper tennisCourtMapper;
private TennisCourtMapper tennisCourtMapper;

public ScheduleDTO addSchedule(Long tennisCourtId, CreateScheduleRequestDTO createScheduleRequestDTO) {
ScheduleDTO newSchedule = new ScheduleDTO();
newSchedule.setTennisCourtId(tennisCourtId);
newSchedule.setStartDateTime(createScheduleRequestDTO.getStartDateTime());
newSchedule.setEndDateTime(createScheduleRequestDTO.getStartDateTime().plusHours(1L));
newSchedule.setTennisCourt(tennisCourtRepository.findById(tennisCourtId).map(tennisCourtMapper::map).orElseThrow(() -> {
throw new EntityNotFoundException("Tennis Court not found.");
}));
ScheduleDTO newSchedule = ScheduleDTO.builder()
.tennisCourtId(tennisCourtId)
.startDateTime(createScheduleRequestDTO.getStartDateTime())
.endDateTime(createScheduleRequestDTO.getStartDateTime().plusHours(1L))
.tennisCourt(tennisCourtRepository.findById(tennisCourtId).map(tennisCourtMapper::map).orElseThrow(() -> {
throw new EntityNotFoundException("Tennis Court not found.");
}))
.build();

return scheduleMapper.map(scheduleRepository.saveAndFlush(scheduleMapper.map(newSchedule)));
}
Expand All @@ -42,7 +43,7 @@ public List<ScheduleDTO> findSchedulesByDates(LocalDateTime startDate, LocalDate

public ScheduleDTO findSchedule(Long scheduleId) {
return scheduleRepository.findById(scheduleId).map(scheduleMapper::map).orElseThrow(() -> {
throw new EntityNotFoundException("Schedule not found.");
throw new EntityNotFoundException("Schedule " + scheduleId + " not found.");
});
}

Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/tenniscourts/tenniscourts/TennisCourt.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.tenniscourts.config.persistence.BaseEntity;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NoArgsConstructor;
Expand All @@ -19,6 +20,7 @@
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Builder
@EqualsAndHashCode(callSuper = true)
@ToString
public class TennisCourt extends BaseEntity<Long> {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
package com.tenniscourts.tenniscourts;

import java.time.LocalDateTime;

import com.tenniscourts.config.BaseRestController;
import lombok.AllArgsConstructor;

import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.ResponseEntity;
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.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.annotations.Api;
Expand All @@ -27,6 +32,16 @@ public ResponseEntity<Void> addTennisCourt(@RequestBody TennisCourtDTO tennisCou
return ResponseEntity.created(locationByEntity(tennisCourtService.addTennisCourt(tennisCourtDTO).getId())).build();
}

@PostMapping("/tennisCourt/createScheduleSlots/{id}")
@ApiOperation("Creates schedule slots from start date time to end date time and returns a tennis court with a list of this slots.")
public ResponseEntity<TennisCourtDTO> addScheduleSlotsToTennisCourt(
@PathVariable("id") Long id,
@RequestParam("startDateTime") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime startDateTime,
@RequestParam("endDateTime") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime endDateTime
) {
return ResponseEntity.ok(tennisCourtService.addScheduleSlotsToTennisCourt(id, startDateTime, endDateTime));
}

@GetMapping("/{id}")
@ApiOperation("Returns a specific tennis court by their identifier. 404 if does not exist.")
public ResponseEntity<TennisCourtDTO> findTennisCourtById(@PathVariable("id") Long id) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.Singular;
import lombok.ToString;

import javax.validation.constraints.NotNull;
Expand All @@ -31,6 +32,7 @@ public class TennisCourtDTO {
private String name;

@ApiModelProperty(notes = "List of schedules of the tennis court.", required = false, position = 2)
@Singular
private List<ScheduleDTO> tennisCourtSchedules;

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
package com.tenniscourts.tenniscourts;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;

import com.tenniscourts.exceptions.EntityNotFoundException;
import com.tenniscourts.schedules.CreateScheduleRequestDTO;
import com.tenniscourts.schedules.ScheduleDTO;
import com.tenniscourts.schedules.ScheduleService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
Expand All @@ -19,9 +26,25 @@ public TennisCourtDTO addTennisCourt(TennisCourtDTO tennisCourt) {
return tennisCourtMapper.map(tennisCourtRepository.saveAndFlush(tennisCourtMapper.map(tennisCourt)));
}

public TennisCourtDTO addScheduleSlotsToTennisCourt(Long id, LocalDateTime startDate, LocalDateTime endDate) {
TennisCourtDTO tennisCourt = findTennisCourtById(id);
long hours = ChronoUnit.HOURS.between(startDate, endDate);

tennisCourt.setTennisCourtSchedules(new ArrayList<>());

for (int hoursIndex = 0; hoursIndex <= hours; hoursIndex++) {
LocalDateTime date = startDate;
CreateScheduleRequestDTO createDTO = CreateScheduleRequestDTO.builder().tennisCourtId(id).startDateTime(date.plusHours(hoursIndex)).build();
ScheduleDTO schedule = scheduleService.addSchedule(id, createDTO);
tennisCourt.getTennisCourtSchedules().add(schedule);
}

return tennisCourt;
}

public TennisCourtDTO findTennisCourtById(Long id) {
return tennisCourtRepository.findById(id).map(tennisCourtMapper::map).orElseThrow(() -> {
throw new EntityNotFoundException("Tennis Court not found.");
throw new EntityNotFoundException("Tennis Court " + id + " not found.");
});
}

Expand Down
Loading