Skip to content

Commit

Permalink
adding booking component and api
Browse files Browse the repository at this point in the history
  • Loading branch information
yelmorabit committed Jul 26, 2020
1 parent 2187c71 commit 624be0c
Show file tree
Hide file tree
Showing 21 changed files with 528 additions and 72 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@
import org.springframework.data.jpa.repository.*;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
* Spring Data repository for the Seat entity.
*/
@SuppressWarnings("unused")
@Repository
public interface SeatRepository extends JpaRepository<Seat, Long> {
List<Seat> findAllByAreaId(final Long area_id);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.elmorabit.battlebrain.service;

import com.elmorabit.battlebrain.service.dto.BookingDTO;
import com.elmorabit.battlebrain.service.dto.SeatDTO;

import java.util.List;

/**
* Service Interface for managing {@link com.elmorabit.battlebrain.domain.Reservation}.
*/
public interface BookingService {

/**
* Get all the reservations.
*
* @return the list of entities.
*/
List<SeatDTO> getAllAvailableSeats(BookingDTO bookingDTO);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package com.elmorabit.battlebrain.service.dto;

import javax.validation.constraints.NotNull;
import java.io.Serializable;
import java.time.Instant;

/**
* A DTO for the {@link com.elmorabit.battlebrain.domain.Reservation} entity.
*/
public class BookingDTO implements Serializable {

private Long id;

@NotNull
private Instant startDate;

@NotNull
private Instant endDate;


private Long areaId;
public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public Instant getStartDate() {
return startDate;
}

public void setStartDate(Instant startDate) {
this.startDate = startDate;
}

public Instant getEndDate() {
return endDate;
}

public void setEndDate(Instant endDate) {
this.endDate = endDate;
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof BookingDTO)) {
return false;
}

return id != null && id.equals(((BookingDTO) o).id);
}

@Override
public int hashCode() {
return 31;
}

// prettier-ignore
@Override
public String toString() {
return "ReservationDTO{" +
"id=" + getId() +
", startDate='" + getStartDate() + "'" +
", endDate='" + getEndDate() + "'" +
"}";
}

public Long getAreaId() {
return areaId;
}

public void setAreaId(final Long areaId) {
this.areaId = areaId;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.elmorabit.battlebrain.service.impl;

import com.elmorabit.battlebrain.domain.Reservation;
import com.elmorabit.battlebrain.domain.Seat;
import com.elmorabit.battlebrain.repository.ReservationRepository;
import com.elmorabit.battlebrain.repository.SeatRepository;
import com.elmorabit.battlebrain.service.BookingService;
import com.elmorabit.battlebrain.service.dto.BookingDTO;
import com.elmorabit.battlebrain.service.dto.SeatDTO;
import com.elmorabit.battlebrain.service.mapper.ReservationMapper;
import com.elmorabit.battlebrain.service.mapper.SeatMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
* Service Implementation for managing {@link Reservation}.
*/
@Service
@Transactional
public class BookingServiceImpl implements BookingService {

private final Logger log = LoggerFactory.getLogger(BookingServiceImpl.class);

private final ReservationRepository reservationRepository;

private final ReservationMapper reservationMapper;

private final SeatRepository seatRepository;

private final SeatMapper seatMapper;


public BookingServiceImpl(ReservationRepository reservationRepository, ReservationMapper reservationMapper, final SeatRepository seatRepository, final SeatMapper seatMapper) {
this.reservationRepository = reservationRepository;
this.reservationMapper = reservationMapper;
this.seatRepository = seatRepository;
this.seatMapper = seatMapper;
}

@Override
public List<SeatDTO> getAllAvailableSeats(final BookingDTO bookingDTO) {


List<Seat> seatList;

if (Objects.nonNull(bookingDTO.getAreaId())) {
seatList = seatRepository.findAllByAreaId(bookingDTO.getAreaId());
} else {
seatList = seatRepository.findAll();
}

return seatList.stream().map(seatMapper::toDto).collect(Collectors.toList());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package com.elmorabit.battlebrain.web.rest;

import com.elmorabit.battlebrain.service.BookingService;
import com.elmorabit.battlebrain.service.dto.BookingDTO;
import com.elmorabit.battlebrain.service.dto.SeatDTO;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.ValidationException;
import java.net.URI;
import java.net.URISyntaxException;
import java.time.Instant;
import java.util.List;
import java.util.Objects;

/**
* REST controller for managing {@link com.elmorabit.battlebrain.domain.Reservation}.
*/
@RestController
@RequestMapping("/api")
public class BookingController {

private final Logger log = LoggerFactory.getLogger(BookingController.class);

private static final String ENTITY_NAME = "reservation";

@Value("${jhipster.clientApp.name}")
private String applicationName;

private final BookingService bookingService;

public BookingController(BookingService bookingService) {
this.bookingService = bookingService;
}

/**
* {@code POST /reservations} : Create a new reservation.
*
* @param bookingDTO the bookingDTO to create.
* @return the {@link ResponseEntity} with status {@code 201 (Created)} and with body the new bookingDTO, or with status {@code 400 (Bad Request)} if the reservation has already an ID.
* @throws URISyntaxException if the Location URI syntax is incorrect.
*/
@PostMapping("/booking")
public ResponseEntity<List<SeatDTO>> createReservation(@Valid @RequestBody BookingDTO bookingDTO) throws URISyntaxException {
log.debug("REST request to save Reservation : {}", bookingDTO);
final Instant startDate = bookingDTO.getStartDate();
final Instant endDate = bookingDTO.getEndDate();
if (Objects.isNull(startDate) || Objects.isNull(endDate) || startDate.compareTo(Instant.now()) <= 0 || startDate.compareTo(endDate) >= 0) {
throw new ValidationException("invalid data");
}
List<SeatDTO> result = bookingService.getAllAvailableSeats(bookingDTO);
return ResponseEntity.ok(result);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,27 +52,6 @@
</changeSet>
<!-- jhipster-needle-liquibase-add-changeset - JHipster will add changesets here -->

<!--
Load sample data generated with Faker.js
- This data can be easily edited using a CSV editor (or even MS Excel) and
is located in the 'src/main/resources/config/liquibase/fake-data' directory
- By default this data is applied when running with the JHipster 'dev' profile.
This can be customized by adding or removing 'faker' in the 'spring.liquibase.contexts'
Spring Boot configuration key.
-->
<changeSet id="20200725130021-1-data" author="jhipster" context="faker">
<loadData
file="config/liquibase/data/seat.csv"
separator=";"
tableName="seat">
<column name="id" type="numeric"/>
<column name="number" type="numeric"/>
<column name="status" type="string"/>
<column name="aisle_seat" type="boolean"/>
<column name="window_seat" type="boolean"/>
<column name="middle_seat" type="boolean"/>
<!-- jhipster-needle-liquibase-add-loadcolumn - JHipster (and/or extensions) can add load columns here -->
</loadData>
</changeSet>


</databaseChangeLog>
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
Added the constraints for entity Seat.
-->
<changeSet id="20200725130021-2" author="jhipster">

<addForeignKeyConstraint baseColumnNames="right_seat_id"
baseTableName="seat"
constraintName="fk_seat_right_seat_id"
Expand All @@ -33,4 +33,30 @@
referencedTableName="area"/>

</changeSet>

<changeSet id="20200725130021-1-data" author="jhipster">
<loadData
file="config/liquibase/data/seat.csv"
separator=";"
tableName="seat">
<column name="id" type="numeric"/>
<column name="number" type="numeric"/>
<column name="status" type="string"/>
<column name="aisle_seat" type="boolean"/>
<column name="window_seat" type="boolean"/>
<column name="middle_seat" type="boolean"/>
<column name="area_id" type="numeric"/>
<!-- jhipster-needle-liquibase-add-loadcolumn - JHipster (and/or extensions) can add load columns here -->
</loadData>
<loadUpdateData
file="config/liquibase/data/seat-to-seat.csv"
separator=";"
tableName="seat" primaryKey="id" onlyUpdate="true">
<column name="id" type="numeric"/>
<column name="right_seat_id" type="numeric"/>
<column name="left_seat_id" type="numeric"/>
<column name="front_seat_id" type="numeric"/>
<!-- jhipster-needle-liquibase-add-loadcolumn - JHipster (and/or extensions) can add load columns here -->
</loadUpdateData>
</changeSet>
</databaseChangeLog>
2 changes: 1 addition & 1 deletion src/main/resources/config/liquibase/data/area.csv
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
id;name
1;Sale Newton
2;Sale Enshtien
2;Sale Einstein
3;Sale Rabat
48 changes: 48 additions & 0 deletions src/main/resources/config/liquibase/data/seat-to-seat.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
id ;right_seat_id ; left_seat_id;front_seat_id
1 ;null;null;null
2 ;null;null;null
3 ;null;null;null
4 ;null;null;null
5 ;null;null;null
6 ;null;null;null
7 ;null;null;null
8 ;null;null;null
9 ;null;null;null
10 ;null;null;null
11 ;null;null;null
12 ;null;null;null
13 ;null;null;null
14 ;null;null;null
21 ;null;null;null
22 ;null;null;null
23 ;null;null;null
24 ;null;null;null
25 ;null;null;null
26 ;null;null;null
27 ;null;null;null
28 ;null;null;null
29 ;null;null;null
210 ;null;null;null
211 ;null;null;null
212 ;null;null;null
111 ;112 ;null;114
112 ;113 ;111 ;115
113 ;null;112 ;116
114 ;115 ;null;111
115 ;116 ;114 ;112
116 ;null;115 ;113
117 ;118 ;null;119
118 ;117 ;null;1110
119 ;null;1110;117
1110;119 ;null;118
1111;null;1112;1113
1112;1111;null;1114
1113;1114;null;1111
1114;null;1113;1112
1115;null;1116;1118
1116;1115;1117;1119
1117;1116;null;1120
1118;1119;null;1115
1119;1120;1118;1116
1120;null;1119;1117

Loading

0 comments on commit 624be0c

Please sign in to comment.