Skip to content

Commit b583cfe

Browse files
committed
CRUD operation for houses
Implemented the following APIs * /communities/{id}/houses - POST - Add house to a community * /communities/{id}/houses - GET - List all houses of a community Closes jmprathab#20
1 parent 9dcce7f commit b583cfe

15 files changed

+248
-56
lines changed

Diff for: docker-compose.yml

+1-5
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,8 @@ services:
77
context: .
88
dockerfile: Dockerfile
99
container_name: "myhome-service"
10-
networks:
11-
- myhome
1210
expose:
13-
- "80"
11+
- "8080"
1412
ports:
1513
- "8080:8080"
1614

17-
networks:
18-
myhome:

Diff for: postman/MyHome.postman_collection.json

+63
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,69 @@
242242
}
243243
},
244244
"response": []
245+
},
246+
{
247+
"name": "List houses of a community",
248+
"request": {
249+
"method": "GET",
250+
"header": [
251+
{
252+
"key": "Authorization",
253+
"type": "text",
254+
"value": "Bearer {{AUTH_TOKEN}}"
255+
}
256+
],
257+
"url": {
258+
"raw": "http://{{GATEWAY_IP}}:{{GATEWAY_PORT}}/communities/{{COMMUNITY_ID}}/houses",
259+
"protocol": "http",
260+
"host": [
261+
"{{GATEWAY_IP}}"
262+
],
263+
"port": "{{GATEWAY_PORT}}",
264+
"path": [
265+
"communities",
266+
"{{COMMUNITY_ID}}",
267+
"houses"
268+
]
269+
}
270+
},
271+
"response": []
272+
},
273+
{
274+
"name": "Add house to community",
275+
"request": {
276+
"method": "POST",
277+
"header": [
278+
{
279+
"key": "Authorization",
280+
"type": "text",
281+
"value": "Bearer {{AUTH_TOKEN}}"
282+
}
283+
],
284+
"body": {
285+
"mode": "raw",
286+
"raw": "{\n \"house\": {\n \"houseId\": \"default-house-id-for-testing\",\n \"name\": \"Default House Name\"\n }\n}",
287+
"options": {
288+
"raw": {
289+
"language": "json"
290+
}
291+
}
292+
},
293+
"url": {
294+
"raw": "http://{{GATEWAY_IP}}:{{GATEWAY_PORT}}/communities/{{COMMUNITY_ID}}/houses",
295+
"protocol": "http",
296+
"host": [
297+
"{{GATEWAY_IP}}"
298+
],
299+
"port": "{{GATEWAY_PORT}}",
300+
"path": [
301+
"communities",
302+
"{{COMMUNITY_ID}}",
303+
"houses"
304+
]
305+
}
306+
},
307+
"response": []
245308
}
246309
],
247310
"protocolProfileBehavior": {}

Diff for: src/main/java/com/prathab/communityservice/controllers/CommunityController.java

+35
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,14 @@
1818

1919
import com.prathab.communityservice.controllers.models.mapper.CommunityApiMapper;
2020
import com.prathab.communityservice.controllers.models.request.AddCommunityAdminRequest;
21+
import com.prathab.communityservice.controllers.models.request.AddCommunityHouseRequest;
2122
import com.prathab.communityservice.controllers.models.request.CreateCommunityRequest;
2223
import com.prathab.communityservice.controllers.models.response.AddCommunityAdminResponse;
24+
import com.prathab.communityservice.controllers.models.response.AddCommunityHouseResponse;
2325
import com.prathab.communityservice.controllers.models.response.CreateCommunityResponse;
2426
import com.prathab.communityservice.controllers.models.response.GetAdminDetailsResponse;
2527
import com.prathab.communityservice.controllers.models.response.GetCommunityDetailsResponse;
28+
import com.prathab.communityservice.controllers.models.response.GetHouseDetailsResponse;
2629
import com.prathab.communityservice.domain.CommunityAdmin;
2730
import com.prathab.communityservice.services.CommunityService;
2831
import java.util.Set;
@@ -112,6 +115,21 @@ public ResponseEntity<Set<GetAdminDetailsResponse>> listCommunityAdmins(
112115
return ResponseEntity.status(HttpStatus.OK).body(getAdminDetailsResponseSet);
113116
}
114117

118+
@GetMapping(
119+
path = "/communities/{communityId}/houses",
120+
produces = {MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE}
121+
)
122+
public ResponseEntity<GetHouseDetailsResponse> listCommunityHouses(
123+
@PathVariable String communityId) {
124+
log.trace("Received request to list all houses of community with id[{}]", communityId);
125+
var houseDetails = communityService.getCommunityDetailsById(communityId).getHouses();
126+
var getHouseDetailsResponseSet =
127+
communityApiMapper.communityHouseSetToCommunityHouseDtoSet(houseDetails);
128+
var response = new GetHouseDetailsResponse();
129+
response.setHouses(getHouseDetailsResponseSet);
130+
return ResponseEntity.status(HttpStatus.OK).body(response);
131+
}
132+
115133
@PostMapping(
116134
path = "/communities/{communityId}/admins",
117135
produces = {MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE},
@@ -128,4 +146,21 @@ public ResponseEntity<AddCommunityAdminResponse> addCommunityAdmin(
128146
response.setAdmins(adminsSet);
129147
return ResponseEntity.status(HttpStatus.CREATED).body(response);
130148
}
149+
150+
@PostMapping(
151+
path = "/communities/{communityId}/houses",
152+
produces = {MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE},
153+
consumes = {MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE}
154+
)
155+
public ResponseEntity<AddCommunityHouseResponse> addCommunityHouse(
156+
@PathVariable String communityId, @Valid @RequestBody
157+
AddCommunityHouseRequest request) {
158+
log.trace("Received request to add house to community with id[{}]", communityId);
159+
160+
var communityHouse = communityApiMapper.communityHouseDtoToCommunityHouse(request.getHouse());
161+
var houseId = communityService.addHouseToCommunity(communityId, communityHouse);
162+
var response = new AddCommunityHouseResponse();
163+
response.setHouseId(houseId);
164+
return ResponseEntity.status(HttpStatus.CREATED).body(response);
165+
}
131166
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.prathab.communityservice.controllers.dto;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.Getter;
5+
import lombok.NoArgsConstructor;
6+
import lombok.Setter;
7+
8+
@Getter
9+
@Setter
10+
@AllArgsConstructor
11+
@NoArgsConstructor
12+
public class CommunityHouseDto {
13+
private String houseId;
14+
private String name;
15+
}

Diff for: src/main/java/com/prathab/communityservice/controllers/models/mapper/CommunityApiMapper.java

+12
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,14 @@
1616

1717
package com.prathab.communityservice.controllers.models.mapper;
1818

19+
import com.prathab.communityservice.controllers.dto.CommunityHouseDto;
1920
import com.prathab.communityservice.controllers.models.request.CreateCommunityRequest;
2021
import com.prathab.communityservice.controllers.models.response.CreateCommunityResponse;
2122
import com.prathab.communityservice.controllers.models.response.GetAdminDetailsResponse;
2223
import com.prathab.communityservice.controllers.models.response.GetCommunityDetailsResponse;
2324
import com.prathab.communityservice.domain.Community;
2425
import com.prathab.communityservice.domain.CommunityAdmin;
26+
import com.prathab.communityservice.domain.CommunityHouse;
2527
import com.prathab.communityservice.dto.CommunityDto;
2628
import java.util.Set;
2729
import org.mapstruct.Mapper;
@@ -49,4 +51,14 @@ Set<GetCommunityDetailsResponse> communitySetToGetCommunityDetailsResponseSet(
4951

5052
Set<GetAdminDetailsResponse> communityAdminSetToGetAdminDetailsResponseSet(
5153
Set<CommunityAdmin> communityAdminSet);
54+
55+
CommunityHouse communityHouseDtoToCommunityHouse(CommunityHouseDto communityHouseDto);
56+
57+
CommunityHouseDto communityHouseToCommunityHouseDto(CommunityHouse communityHouse);
58+
59+
Set<CommunityHouseDto> communityHouseSetToCommunityHouseDtoSet(
60+
Set<CommunityHouse> communityHouse);
61+
62+
Set<CommunityHouse> communityHouseDtoSetToCommunityHouseSet(
63+
Set<CommunityHouseDto> communityHouseDto);
5264
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Copyright 2020 Prathab Murugan
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.prathab.communityservice.controllers.models.request;
18+
19+
import com.prathab.communityservice.controllers.dto.CommunityHouseDto;
20+
import javax.validation.constraints.NotNull;
21+
import lombok.AllArgsConstructor;
22+
import lombok.Getter;
23+
import lombok.NoArgsConstructor;
24+
import lombok.Setter;
25+
26+
@NoArgsConstructor
27+
@AllArgsConstructor
28+
@Getter
29+
@Setter
30+
public class AddCommunityHouseRequest {
31+
@NotNull
32+
private CommunityHouseDto house;
33+
}

Diff for: src/main/java/com/prathab/communityservice/services/CommunityAdminService.java renamed to src/main/java/com/prathab/communityservice/controllers/models/response/AddCommunityHouseResponse.java

+11-5
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,17 @@
1414
* limitations under the License.
1515
*/
1616

17-
package com.prathab.communityservice.services;
17+
package com.prathab.communityservice.controllers.models.response;
1818

19-
import com.prathab.communityservice.domain.Community;
20-
import com.prathab.communityservice.dto.CommunityAdminDto;
19+
import lombok.AllArgsConstructor;
20+
import lombok.Getter;
21+
import lombok.NoArgsConstructor;
22+
import lombok.Setter;
2123

22-
public interface CommunityAdminService {
23-
Community addCommunityAdmin(String communityId, CommunityAdminDto communityAdminDto);
24+
@NoArgsConstructor
25+
@AllArgsConstructor
26+
@Getter
27+
@Setter
28+
public class AddCommunityHouseResponse {
29+
private String houseId;
2430
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.prathab.communityservice.controllers.models.response;
2+
3+
import com.prathab.communityservice.controllers.dto.CommunityHouseDto;
4+
import java.util.Set;
5+
import lombok.AllArgsConstructor;
6+
import lombok.Getter;
7+
import lombok.NoArgsConstructor;
8+
import lombok.Setter;
9+
10+
@Getter
11+
@Setter
12+
@NoArgsConstructor
13+
@AllArgsConstructor
14+
public class GetHouseDetailsResponse {
15+
private Set<CommunityHouseDto> houses;
16+
}

Diff for: src/main/java/com/prathab/communityservice/domain/Community.java

+4
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import javax.persistence.Entity;
2323
import javax.persistence.FetchType;
2424
import javax.persistence.ManyToMany;
25+
import javax.persistence.OneToMany;
2526
import lombok.AllArgsConstructor;
2627
import lombok.Getter;
2728
import lombok.NoArgsConstructor;
@@ -41,6 +42,9 @@ public class Community extends CommunityBaseEntity {
4142
joinColumns = @JoinColumn(name = "community_id"),
4243
inverseJoinColumns = @JoinColumn(name = "admin_id"))*/
4344
private Set<CommunityAdmin> admins = new HashSet<>();
45+
46+
@OneToMany
47+
private Set<CommunityHouse> houses = new HashSet<>();
4448
@Column(nullable = false)
4549
private String name;
4650
@Column(unique = true, nullable = false)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.prathab.communityservice.domain;
2+
3+
import javax.persistence.Column;
4+
import javax.persistence.Entity;
5+
import javax.persistence.ManyToOne;
6+
import lombok.AllArgsConstructor;
7+
import lombok.Getter;
8+
import lombok.NoArgsConstructor;
9+
import lombok.Setter;
10+
11+
@Entity
12+
@AllArgsConstructor
13+
@NoArgsConstructor
14+
@Getter
15+
@Setter
16+
public class CommunityHouse extends CommunityBaseEntity {
17+
@ManyToOne
18+
private Community community;
19+
@Column(nullable = false)
20+
private String name;
21+
@Column(nullable = false)
22+
private String houseId;
23+
}

Diff for: src/main/java/com/prathab/communityservice/dto/mapper/CommunityMapper.java

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616

1717
package com.prathab.communityservice.dto.mapper;
18+
1819
import com.prathab.communityservice.domain.Community;
1920
import com.prathab.communityservice.dto.CommunityDto;
2021
import java.util.Set;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.prathab.communityservice.repositories;
2+
3+
import com.prathab.communityservice.domain.CommunityHouse;
4+
import org.springframework.data.repository.CrudRepository;
5+
import org.springframework.stereotype.Repository;
6+
7+
@Repository
8+
public interface CommunityHouseRepository extends CrudRepository<CommunityHouse, Long> {
9+
CommunityHouse findByHouseId(String houseId);
10+
}

Diff for: src/main/java/com/prathab/communityservice/services/CommunityService.java

+3
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package com.prathab.communityservice.services;
1818

1919
import com.prathab.communityservice.domain.Community;
20+
import com.prathab.communityservice.domain.CommunityHouse;
2021
import com.prathab.communityservice.dto.CommunityDto;
2122
import java.util.Set;
2223

@@ -28,4 +29,6 @@ public interface CommunityService {
2829
Community getCommunityDetailsById(String communityId);
2930

3031
Community addAdminsToCommunity(String communityId, Set<String> admins);
32+
33+
String addHouseToCommunity(String communityId, CommunityHouse house);
3134
}

Diff for: src/main/java/com/prathab/communityservice/services/springdatajpa/CommunityAdminSDJpaService.java

-43
This file was deleted.

0 commit comments

Comments
 (0)