Skip to content
This repository has been archived by the owner on Nov 19, 2024. It is now read-only.

feature/issue-#46--CAPTURE_DURATION_OF_STAY #278

Open
wants to merge 3 commits 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
157 changes: 155 additions & 2 deletions api/src/main/resources/public/swagger/api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -892,7 +892,66 @@ paths:
description: If house member was removed from house
'400':
description: If params are invalid

/houses/{houseId}/rentals:
post:
tags:
- Houses
description: Add the duration of stay to house history.
operationId: captureStay
parameters:
- in: path
name: houseId
schema:
type: string
required: true
requestBody:
content:
application/json:
schema:
$ref: '#/components/schemas/RentalRequest'
application/xml:
schema:
$ref: '#/components/schemas/RentalRequest'
required: true
responses:
'201':
description: If duration of stay was added to house history
content:
application/json:
schema:
$ref: '#/components/schemas/RentalResponse'
application/xml:
schema:
$ref: '#/components/schemas/RentalResponse'
'404':
description: If parameters are invalid
get:
tags:
- Houses
operationId: listRentalsForHouseId
parameters:
- in: query
name: pageable
required: false
schema:
$ref: '#/components/schemas/Pageable'
- in: path
name: houseId
schema:
type: string
required: true
responses:
'200':
description: If history is present
content:
application/json:
schema:
$ref: '#/components/schemas/ListRentalsResponse'
application/xml:
schema:
$ref: '#/components/schemas/ListRentalsResponse'
'404':
description: If params are invalid
/payments:
post:
security:
Expand Down Expand Up @@ -1400,4 +1459,98 @@ components:
email:
type: string
password:
type: string
type: string
ListRentalsResponse:
type: object
properties:
rentals:
type: array
items:
$ref: '#/components/schemas/RentalDto'
RentalResponse:
type: object
required:
- memberId
- houseId
- stayFromDate
- stayToDate
- bookingFromDate
- bookingToDate
properties:
memberId:
type: string
houseId:
type: string
stayFromDate:
type: string
format: date-time
description: Creation date and time
example: "2021-01-30T08:30:00Z"
stayToDate:
type: string
format: date-time
description: Creation date and time
example: "2021-01-30T08:30:00Z"
bookingFromDate:
type: string
format: date-time
description: Creation date and time
example: "2021-01-30T08:30:00Z"
bookingToDate:
type: string
format: date-time
description: Creation date and time
example: "2021-01-30T08:30:00Z"
RentalDto:
type: object
required:
- memberId
- houseId
- stayFromDate
- stayToDate
- bookingFromDate
- bookingToDate
properties:
memberId:
type: string
houseId:
type: string
stayFromDate:
type: string
format: date-time
description: Creation date and time
example: "2021-01-30T08:30:00Z"
stayToDate:
type: string
format: date-time
description: Creation date and time
example: "2021-01-30T08:30:00Z"
bookingFromDate:
type: string
format: date-time
description: Creation date and time
example: "2021-01-30T08:30:00Z"
bookingToDate:
type: string
format: date-time
description: Creation date and time
example: "2021-01-30T08:30:00Z"
RentalRequest:
type: object
required:
- memberId
- bookingFromDate
- bookingToDate
properties:
memberId:
type: string
bookingFromDate:
type: string
format: date-time
description: Creation date and time
example: "2021-01-30T08:30:00Z"
bookingToDate:
type: string
format: date-time
description: Creation date and time
example: "2021-01-30T08:30:00Z"
46 changes: 38 additions & 8 deletions service/src/main/java/com/myhome/controllers/HouseController.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,16 @@

import com.myhome.api.HousesApi;
import com.myhome.controllers.dto.mapper.HouseMemberMapper;
import com.myhome.controllers.dto.mapper.HouseRentalMapperImpl;
import com.myhome.controllers.mapper.HouseApiMapper;
import com.myhome.domain.CommunityHouse;
import com.myhome.domain.HouseMember;
import com.myhome.model.AddHouseMemberRequest;
import com.myhome.model.AddHouseMemberResponse;
import com.myhome.model.GetHouseDetailsResponse;
import com.myhome.model.GetHouseDetailsResponseCommunityHouse;
import com.myhome.model.ListHouseMembersResponse;
import com.myhome.domain.HouseRental;
import com.myhome.model.*;
import com.myhome.services.HouseService;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

import java.util.*;
import java.util.stream.Collectors;
import javax.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -45,6 +43,7 @@
@Slf4j
public class HouseController implements HousesApi {
private final HouseMemberMapper houseMemberMapper;
private final HouseRentalMapperImpl houseRentalMapper;
private final HouseService houseService;
private final HouseApiMapper houseApiMapper;

Expand Down Expand Up @@ -90,6 +89,23 @@ public ResponseEntity<ListHouseMembersResponse> listAllMembersOfHouse(
.orElse(ResponseEntity.notFound().build());
}

@Override
public ResponseEntity<ListRentalsResponse> listRentalsForHouseId(String houseId, @PageableDefault(size = 200) Pageable pageable) {
log.trace("Received request to list rentals for house with house id[{}]",
houseId);
Optional<List<HouseRental>> houseRentals = houseService.listHouseRentalsForHouseId(houseId, pageable);
if(houseRentals.isPresent()){
List<RentalDto> rentalDtos = houseRentals.get().stream()
.map(houseRentalMapper::HouseRentalToRentalDto)
.collect(Collectors.toList());
ListRentalsResponse rentalsResponse = new ListRentalsResponse();
rentalsResponse.setRentals(rentalDtos);
return ResponseEntity.ok().body(rentalsResponse);
}else {
return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
}
}

@Override
public ResponseEntity<AddHouseMemberResponse> addHouseMembers(
@PathVariable String houseId, @Valid AddHouseMemberRequest request) {
Expand All @@ -109,6 +125,20 @@ public ResponseEntity<AddHouseMemberResponse> addHouseMembers(
}
}

@Override
public ResponseEntity<RentalResponse> captureStay(String houseId, RentalRequest rentalRequest) {
log.trace("Received request to capture stay for house with house id[{}] and member id[{}]",
houseId, rentalRequest.getMemberId());
Optional<HouseRental> houseRental = houseService.createRentalForHouseId(
houseId, rentalRequest.getMemberId(),
rentalRequest.getBookingFromDate(),
rentalRequest.getBookingToDate()
);
return houseRental.isPresent() ?
ResponseEntity.ok().body(houseRentalMapper.HouseRentalToRentalResponse(houseRental.get())) :
ResponseEntity.status(HttpStatus.NOT_FOUND).build();
}

@Override
public ResponseEntity<Void> deleteHouseMember(String houseId, String memberId) {
log.trace("Received request to delete a member from house with house id[{}] and member id[{}]",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.myhome.controllers.dto.mapper;

import com.myhome.domain.HouseRental;
import com.myhome.model.RentalDto;
import com.myhome.model.RentalRequest;
import com.myhome.model.RentalResponse;

import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Mappings;

@Mapper
public interface HouseRentalMapper {
RentalResponse RentalDtoToRentalResponse(RentalDto houseHistoryDto);
@Mappings({
@Mapping(target = "houseId", source = "houseId")
})
RentalDto RentalRequestToRentalDto(String houseId, RentalRequest houseHistoryRequest);
RentalDto HouseRentalToRentalDto(HouseRental houseHistory);
RentalResponse HouseRentalToRentalResponse(HouseRental houseHistory);
HouseRental RentalDtoToHouseRental(RentalDto houseHistoryDto);
}
44 changes: 44 additions & 0 deletions service/src/main/java/com/myhome/domain/HouseRental.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.myhome.domain;

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

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.OneToOne;
import java.time.OffsetDateTime;

@Entity
@AllArgsConstructor
@NoArgsConstructor
@Getter
@Setter
public class HouseRental extends BaseEntity {

@Column(unique = false, nullable = false)
private String houseId;

@Column(nullable = false, unique = false)
private String memberId;

@Column(nullable = true)
private OffsetDateTime bookingFromDate;

@Column(nullable = true)
private OffsetDateTime bookingToDate;

@Column(nullable = true)
private OffsetDateTime arrivalDate;

@Column(nullable = true)
private OffsetDateTime departureDate;

@OneToOne(fetch = FetchType.EAGER)
private CommunityHouse communityHouse;

@OneToOne(fetch = FetchType.EAGER)
private HouseMember houseMember;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.myhome.repositories;

import com.myhome.domain.HouseRental;

import java.util.List;
import java.util.Optional;

import org.springframework.data.domain.Pageable;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface HouseHistoryRepository extends PagingAndSortingRepository<HouseRental,Long> {

Optional<List<HouseRental>> findAllByCommunityHouse_HouseId(String houseId, Pageable pageable);
}
7 changes: 7 additions & 0 deletions service/src/main/java/com/myhome/services/HouseService.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@

import com.myhome.domain.CommunityHouse;
import com.myhome.domain.HouseMember;
import java.time.OffsetDateTime;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import com.myhome.domain.HouseRental;
import org.springframework.data.domain.Pageable;

public interface HouseService {
Expand All @@ -37,4 +39,9 @@ public interface HouseService {
Optional<List<HouseMember>> getHouseMembersById(String houseId, Pageable pageable);

Optional<List<HouseMember>> listHouseMembersForHousesOfUserId(String userId, Pageable pageable);

Optional<List<HouseRental>> listHouseRentalsForHouseId(String houseId, Pageable pageable);

Optional<HouseRental> createRentalForHouseId(String houseId, String houseMemberId, OffsetDateTime bookingFromDate, OffsetDateTime bookingToDate);

}
Loading