From 6246d22663a94ed4b3ebf929bf37de2d9ed2d4bf Mon Sep 17 00:00:00 2001 From: Dark-Ultron Date: Mon, 18 Sep 2023 16:05:03 +0530 Subject: [PATCH] Issue Fixed #232: Move endpoints related to houses from community tag and moved associated methods from CommunityController to HouseController --- .../main/resources/public/swagger/api.yaml | 6 +- .../controllers/CommunityController.java | 92 +++++++++---------- .../myhome/controllers/HouseController.java | 61 +++++++++++- 3 files changed, 105 insertions(+), 54 deletions(-) diff --git a/api/src/main/resources/public/swagger/api.yaml b/api/src/main/resources/public/swagger/api.yaml index 2c7c766c..c5045451 100644 --- a/api/src/main/resources/public/swagger/api.yaml +++ b/api/src/main/resources/public/swagger/api.yaml @@ -668,7 +668,7 @@ paths: security: - bearerAuth: [ ] tags: - - Communities + - Houses description: List all houses of the community given a community id operationId: listCommunityHouses parameters: @@ -696,7 +696,7 @@ paths: description: If params are invalid post: tags: - - Communities + - Houses description: Add a new house to the community given a community id operationId: addCommunityHouses parameters: @@ -732,7 +732,7 @@ paths: security: - bearerAuth: [ ] tags: - - Communities + - Houses description: Remove of house from the community given a community id and a house id operationId: removeCommunityHouse parameters: diff --git a/service/src/main/java/com/myhome/controllers/CommunityController.java b/service/src/main/java/com/myhome/controllers/CommunityController.java index 61d12782..1dbdfe92 100644 --- a/service/src/main/java/com/myhome/controllers/CommunityController.java +++ b/service/src/main/java/com/myhome/controllers/CommunityController.java @@ -115,19 +115,19 @@ public ResponseEntity listCommunityAdmins( .orElseGet(() -> ResponseEntity.notFound().build()); } - @Override - public ResponseEntity listCommunityHouses( - @PathVariable String communityId, - @PageableDefault(size = 200) Pageable pageable) { - log.trace("Received request to list all houses of community with id[{}]", communityId); - - return communityService.findCommunityHousesById(communityId, pageable) - .map(HashSet::new) - .map(communityApiMapper::communityHouseSetToRestApiResponseCommunityHouseSet) - .map(houses -> new GetHouseDetailsResponse().houses(houses)) - .map(ResponseEntity::ok) - .orElseGet(() -> ResponseEntity.notFound().build()); - } +// @Override +// public ResponseEntity listCommunityHouses( +// @PathVariable String communityId, +// @PageableDefault(size = 200) Pageable pageable) { +// log.trace("Received request to list all houses of community with id[{}]", communityId); +// +// return communityService.findCommunityHousesById(communityId, pageable) +// .map(HashSet::new) +// .map(communityApiMapper::communityHouseSetToRestApiResponseCommunityHouseSet) +// .map(houses -> new GetHouseDetailsResponse().houses(houses)) +// .map(ResponseEntity::ok) +// .orElseGet(() -> ResponseEntity.notFound().build()); +// } @Override public ResponseEntity addCommunityAdmins( @@ -146,39 +146,39 @@ public ResponseEntity addCommunityAdmins( }).orElse(ResponseEntity.status(HttpStatus.NOT_FOUND).build()); } - @Override - public ResponseEntity addCommunityHouses( - @PathVariable String communityId, @Valid @RequestBody - AddCommunityHouseRequest request) { - log.trace("Received request to add house to community with id[{}]", communityId); - Set houseNames = request.getHouses(); - Set communityHouses = - communityApiMapper.communityHouseNamesSetToCommunityHouseSet(houseNames); - Set houseIds = communityService.addHousesToCommunity(communityId, communityHouses); - if (houseIds.size() != 0 && houseNames.size() != 0) { - AddCommunityHouseResponse response = new AddCommunityHouseResponse(); - response.setHouses(houseIds); - return ResponseEntity.status(HttpStatus.CREATED).body(response); - } else { - return ResponseEntity.status(HttpStatus.BAD_REQUEST).build(); - } - } - - @Override - public ResponseEntity removeCommunityHouse( - @PathVariable String communityId, @PathVariable String houseId - ) { - log.trace( - "Received request to delete house with id[{}] from community with id[{}]", - houseId, communityId); - - Optional communityOptional = communityService.getCommunityDetailsById(communityId); - - return communityOptional.filter( - community -> communityService.removeHouseFromCommunityByHouseId(community, houseId)) - .map(removed -> ResponseEntity.noContent().build()) - .orElseGet(() -> ResponseEntity.notFound().build()); - } +// @Override +// public ResponseEntity addCommunityHouses( +// @PathVariable String communityId, @Valid @RequestBody +// AddCommunityHouseRequest request) { +// log.trace("Received request to add house to community with id[{}]", communityId); +// Set houseNames = request.getHouses(); +// Set communityHouses = +// communityApiMapper.communityHouseNamesSetToCommunityHouseSet(houseNames); +// Set houseIds = communityService.addHousesToCommunity(communityId, communityHouses); +// if (houseIds.size() != 0 && houseNames.size() != 0) { +// AddCommunityHouseResponse response = new AddCommunityHouseResponse(); +// response.setHouses(houseIds); +// return ResponseEntity.status(HttpStatus.CREATED).body(response); +// } else { +// return ResponseEntity.status(HttpStatus.BAD_REQUEST).build(); +// } +// } +// +// @Override +// public ResponseEntity removeCommunityHouse( +// @PathVariable String communityId, @PathVariable String houseId +// ) { +// log.trace( +// "Received request to delete house with id[{}] from community with id[{}]", +// houseId, communityId); +// +// Optional communityOptional = communityService.getCommunityDetailsById(communityId); +// +// return communityOptional.filter( +// community -> communityService.removeHouseFromCommunityByHouseId(community, houseId)) +// .map(removed -> ResponseEntity.noContent().build()) +// .orElseGet(() -> ResponseEntity.notFound().build()); +// } @Override public ResponseEntity removeAdminFromCommunity( diff --git a/service/src/main/java/com/myhome/controllers/HouseController.java b/service/src/main/java/com/myhome/controllers/HouseController.java index 4f9ed9f4..0a6deb10 100644 --- a/service/src/main/java/com/myhome/controllers/HouseController.java +++ b/service/src/main/java/com/myhome/controllers/HouseController.java @@ -18,17 +18,17 @@ import com.myhome.api.HousesApi; import com.myhome.controllers.dto.mapper.HouseMemberMapper; +import com.myhome.controllers.mapper.CommunityApiMapper; import com.myhome.controllers.mapper.HouseApiMapper; +import com.myhome.domain.Community; 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.model.*; +import com.myhome.services.CommunityService; import com.myhome.services.HouseService; import java.util.Collections; import java.util.HashSet; +import java.util.Optional; import java.util.Set; import javax.validation.Valid; import lombok.RequiredArgsConstructor; @@ -38,6 +38,7 @@ import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RestController; @RestController @@ -47,6 +48,8 @@ public class HouseController implements HousesApi { private final HouseMemberMapper houseMemberMapper; private final HouseService houseService; private final HouseApiMapper houseApiMapper; + private final CommunityService communityService; + private final CommunityApiMapper communityApiMapper; @Override public ResponseEntity listAllHouses( @@ -120,4 +123,52 @@ public ResponseEntity deleteHouseMember(String houseId, String memberId) { return ResponseEntity.status(HttpStatus.NOT_FOUND).build(); } } + + @Override + public ResponseEntity addCommunityHouses( + @PathVariable String communityId, @Valid @RequestBody + AddCommunityHouseRequest request) { + log.trace("Received request to add house to community with id[{}]", communityId); + Set houseNames = request.getHouses(); + Set communityHouses = + communityApiMapper.communityHouseNamesSetToCommunityHouseSet(houseNames); + Set houseIds = communityService.addHousesToCommunity(communityId, communityHouses); + if (houseIds.size() != 0 && houseNames.size() != 0) { + AddCommunityHouseResponse response = new AddCommunityHouseResponse(); + response.setHouses(houseIds); + return ResponseEntity.status(HttpStatus.CREATED).body(response); + } else { + return ResponseEntity.status(HttpStatus.BAD_REQUEST).build(); + } + } + + @Override + public ResponseEntity removeCommunityHouse( + @PathVariable String communityId, @PathVariable String houseId + ) { + log.trace( + "Received request to delete house with id[{}] from community with id[{}]", + houseId, communityId); + + Optional communityOptional = communityService.getCommunityDetailsById(communityId); + + return communityOptional.filter( + community -> communityService.removeHouseFromCommunityByHouseId(community, houseId)) + .map(removed -> ResponseEntity.noContent().build()) + .orElseGet(() -> ResponseEntity.notFound().build()); + } + + @Override + public ResponseEntity listCommunityHouses( + @PathVariable String communityId, + @PageableDefault(size = 200) Pageable pageable) { + log.trace("Received request to list all houses of community with id[{}]", communityId); + + return communityService.findCommunityHousesById(communityId, pageable) + .map(HashSet::new) + .map(communityApiMapper::communityHouseSetToRestApiResponseCommunityHouseSet) + .map(houses -> new GetHouseDetailsResponse().houses(houses)) + .map(ResponseEntity::ok) + .orElseGet(() -> ResponseEntity.notFound().build()); + } } \ No newline at end of file