Skip to content

Commit

Permalink
Issue Fixed jmprathab#232: Move endpoints related to houses from comm…
Browse files Browse the repository at this point in the history
…unity tag and moved associated methods from CommunityController to HouseController
  • Loading branch information
Dark-Ultron committed Sep 18, 2023
1 parent 193aa2c commit 6246d22
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 54 deletions.
6 changes: 3 additions & 3 deletions api/src/main/resources/public/swagger/api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -668,7 +668,7 @@ paths:
security:
- bearerAuth: [ ]
tags:
- Communities
- Houses
description: List all houses of the community given a community id
operationId: listCommunityHouses
parameters:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,19 +115,19 @@ public ResponseEntity<ListCommunityAdminsResponse> listCommunityAdmins(
.orElseGet(() -> ResponseEntity.notFound().build());
}

@Override
public ResponseEntity<GetHouseDetailsResponse> 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<GetHouseDetailsResponse> 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<AddCommunityAdminResponse> addCommunityAdmins(
Expand All @@ -146,39 +146,39 @@ public ResponseEntity<AddCommunityAdminResponse> addCommunityAdmins(
}).orElse(ResponseEntity.status(HttpStatus.NOT_FOUND).build());
}

@Override
public ResponseEntity<AddCommunityHouseResponse> addCommunityHouses(
@PathVariable String communityId, @Valid @RequestBody
AddCommunityHouseRequest request) {
log.trace("Received request to add house to community with id[{}]", communityId);
Set<CommunityHouseName> houseNames = request.getHouses();
Set<CommunityHouse> communityHouses =
communityApiMapper.communityHouseNamesSetToCommunityHouseSet(houseNames);
Set<String> 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<Void> removeCommunityHouse(
@PathVariable String communityId, @PathVariable String houseId
) {
log.trace(
"Received request to delete house with id[{}] from community with id[{}]",
houseId, communityId);

Optional<Community> communityOptional = communityService.getCommunityDetailsById(communityId);

return communityOptional.filter(
community -> communityService.removeHouseFromCommunityByHouseId(community, houseId))
.map(removed -> ResponseEntity.noContent().<Void>build())
.orElseGet(() -> ResponseEntity.notFound().build());
}
// @Override
// public ResponseEntity<AddCommunityHouseResponse> addCommunityHouses(
// @PathVariable String communityId, @Valid @RequestBody
// AddCommunityHouseRequest request) {
// log.trace("Received request to add house to community with id[{}]", communityId);
// Set<CommunityHouseName> houseNames = request.getHouses();
// Set<CommunityHouse> communityHouses =
// communityApiMapper.communityHouseNamesSetToCommunityHouseSet(houseNames);
// Set<String> 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<Void> removeCommunityHouse(
// @PathVariable String communityId, @PathVariable String houseId
// ) {
// log.trace(
// "Received request to delete house with id[{}] from community with id[{}]",
// houseId, communityId);
//
// Optional<Community> communityOptional = communityService.getCommunityDetailsById(communityId);
//
// return communityOptional.filter(
// community -> communityService.removeHouseFromCommunityByHouseId(community, houseId))
// .map(removed -> ResponseEntity.noContent().<Void>build())
// .orElseGet(() -> ResponseEntity.notFound().build());
// }

@Override
public ResponseEntity<Void> removeAdminFromCommunity(
Expand Down
61 changes: 56 additions & 5 deletions service/src/main/java/com/myhome/controllers/HouseController.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -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<GetHouseDetailsResponse> listAllHouses(
Expand Down Expand Up @@ -120,4 +123,52 @@ public ResponseEntity<Void> deleteHouseMember(String houseId, String memberId) {
return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
}
}

@Override
public ResponseEntity<AddCommunityHouseResponse> addCommunityHouses(
@PathVariable String communityId, @Valid @RequestBody
AddCommunityHouseRequest request) {
log.trace("Received request to add house to community with id[{}]", communityId);
Set<CommunityHouseName> houseNames = request.getHouses();
Set<CommunityHouse> communityHouses =
communityApiMapper.communityHouseNamesSetToCommunityHouseSet(houseNames);
Set<String> 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<Void> removeCommunityHouse(
@PathVariable String communityId, @PathVariable String houseId
) {
log.trace(
"Received request to delete house with id[{}] from community with id[{}]",
houseId, communityId);

Optional<Community> communityOptional = communityService.getCommunityDetailsById(communityId);

return communityOptional.filter(
community -> communityService.removeHouseFromCommunityByHouseId(community, houseId))
.map(removed -> ResponseEntity.noContent().<Void>build())
.orElseGet(() -> ResponseEntity.notFound().build());
}

@Override
public ResponseEntity<GetHouseDetailsResponse> 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());
}
}

0 comments on commit 6246d22

Please sign in to comment.