Skip to content

Commit 3195f2b

Browse files
committed
solved mapping the GovernmentTranslation into the Representative entity
1 parent 5d15b92 commit 3195f2b

File tree

8 files changed

+135
-20
lines changed

8 files changed

+135
-20
lines changed

api-contract/src/main/resources/api-contract.yaml

Lines changed: 53 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ paths:
7777
'405':
7878
description: Validation exception
7979

80-
/{languageShortName}/api/admin/gov-representatives/government/{governmentId}:
80+
/{languageShortName}/api/admin/gov-representatives/governments/{governmentId}:
8181
get:
8282
tags:
8383
- government-representative
@@ -137,7 +137,7 @@ paths:
137137
'405':
138138
description: Validation exception
139139

140-
/{languageShortName}/api/admin/gov-representatives/government:
140+
/{languageShortName}/api/admin/governments:
141141
get:
142142
tags:
143143
- government
@@ -158,7 +158,47 @@ paths:
158158
schema:
159159
type: array
160160
items:
161-
$ref: '#/components/schemas/GovernmentAdminModel'
161+
$ref: '#/components/schemas/GovernmentTranslationModel'
162+
headers:
163+
Cache-Control:
164+
description: Cache control header
165+
schema:
166+
type: string
167+
Expires:
168+
description: Expiration date header
169+
schema:
170+
type: string
171+
172+
/{languageShortName}/api/admin/governments/{governmentId}:
173+
get:
174+
tags:
175+
- government
176+
summary: Get government data
177+
operationId: renderAllGovernmentsById
178+
parameters:
179+
- name: languageShortName
180+
in: path
181+
description: short name of selected language
182+
required: true
183+
schema:
184+
type: string
185+
- name: governmentId
186+
in: path
187+
description: ID of the government to filter by
188+
required: true
189+
schema:
190+
type: integer
191+
format: int64
192+
minimum: 1
193+
responses:
194+
'200':
195+
description: OK
196+
content:
197+
application/json:
198+
schema:
199+
type: array
200+
items:
201+
$ref: '#/components/schemas/GovernmentTranslationModel'
162202
headers:
163203
Cache-Control:
164204
description: Cache control header
@@ -282,14 +322,22 @@ components:
282322
example: 'ételérzékenység: nincs, egyéb infó: vegán'
283323

284324
GovernmentAdminModel:
325+
type: object
326+
properties:
327+
name:
328+
type: string
329+
languageShortName:
330+
type: string
331+
332+
GovernmentTranslationModel:
285333
type: object
286334
properties:
287335
id:
288336
type: integer
289337
format: int64
290-
name:
338+
language_short_name:
291339
type: string
292-
languageShortName:
340+
name:
293341
type: string
294342

295343
Availability:

government-service/pom.xml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,15 @@
8686
<version>3.0.2</version>
8787
<scope>compile</scope>
8888
</dependency>
89+
<dependency>
90+
<groupId>org.apache.tomcat.embed</groupId>
91+
<artifactId>tomcat-embed-core</artifactId>
92+
<version>10.1.7</version>
93+
</dependency>
94+
<dependency>
95+
<groupId>org.apache.tomcat.embed</groupId>
96+
<artifactId>tomcat-embed-core</artifactId>
97+
</dependency>
8998

9099
</dependencies>
91100

government-service/src/main/java/com/csaba79coder/bestprotocol/controller/GovernmentController.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.csaba79coder.bestprotocol.api.GovernmentApi;
44
import com.csaba79coder.bestprotocol.model.GovernmentAdminModel;
5+
import com.csaba79coder.bestprotocol.model.GovernmentTranslationModel;
56
import com.csaba79coder.bestprotocol.model.government.service.GovernmentService;
67
import lombok.RequiredArgsConstructor;
78
import org.springframework.http.ResponseEntity;
@@ -23,7 +24,12 @@ public ResponseEntity<List<GovernmentAdminModel>> renderAllGovernments() {
2324
}*/
2425

2526
@Override
26-
public ResponseEntity<List<GovernmentAdminModel>> renderAllGovernments(String languageShortName) {
27+
public ResponseEntity<List<GovernmentTranslationModel>> renderAllGovernments(String languageShortName) {
2728
return ResponseEntity.status(200).body(governmentService.findAllGovernmentsByLang(languageShortName));
2829
}
30+
31+
@Override
32+
public ResponseEntity<List<GovernmentTranslationModel>> renderAllGovernmentsById(String languageShortName, Long governmentId) {
33+
return null;
34+
}
2935
}

government-service/src/main/java/com/csaba79coder/bestprotocol/model/government/persistence/GovernmentTranslationRepository.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,6 @@ public interface GovernmentTranslationRepository extends JpaRepository<Governmen
1313
List<GovernmentTranslation> findGovernmentTranslationByLanguageShortNameAndGovernmentId(String languageShortName, Long governmentId);
1414
List<GovernmentTranslation> findGovernmentTranslationByLanguageShortName(String languageShortName);
1515
Optional<GovernmentTranslation> findGovernmentByNameContainsIgnoreCase(String name);
16+
Optional<GovernmentTranslation> findGovernmentTranslationById(Long id);
17+
GovernmentTranslation findByGovernmentIdAndLanguageShortName(Long governmentId, String languageShortName);
1618
}
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,38 @@
11
package com.csaba79coder.bestprotocol.model.government.service;
22

3-
import com.csaba79coder.bestprotocol.model.GovernmentAdminModel;
3+
import com.csaba79coder.bestprotocol.model.GovernmentTranslationModel;
44
import com.csaba79coder.bestprotocol.model.government.entity.GovernmentTranslation;
5-
import com.csaba79coder.bestprotocol.model.government.persistence.GovernmentRepository;
65
import com.csaba79coder.bestprotocol.model.government.persistence.GovernmentTranslationRepository;
76
import com.csaba79coder.bestprotocol.util.mapper.Mapper;
87
import lombok.RequiredArgsConstructor;
8+
import lombok.extern.slf4j.Slf4j;
99
import org.springframework.stereotype.Service;
1010

1111
import java.util.List;
12+
import java.util.NoSuchElementException;
1213
import java.util.stream.Collectors;
1314

1415
@Service
1516
@RequiredArgsConstructor
17+
@Slf4j
1618
public class GovernmentService {
1719

18-
private final GovernmentRepository governmentRepository;
1920
private final GovernmentTranslationRepository governmentTranslationRepository;
2021

21-
public List<GovernmentAdminModel> findAllGovernments() {
22-
return governmentRepository.findAll()
22+
public List<GovernmentTranslationModel> findAllGovernmentsByLang(String lang) {
23+
// TODO from Translation creating immediately the Model!
24+
return governmentTranslationRepository.findGovernmentTranslationByLanguageShortName(lang)
2325
.stream()
24-
.map(Mapper::mapGovernmentEntityToAdminModel)
26+
.map(Mapper::mapGovernmentTranslationToGovernmentAdminModel)
2527
.collect(Collectors.toList());
2628
}
2729

28-
public List<GovernmentAdminModel> findAllGovernmentsByLang(String lang) {
29-
return governmentTranslationRepository.findGovernmentTranslationByLanguageShortName(lang)
30-
.stream()
31-
.map(GovernmentTranslation::getGovernment)
32-
.map(Mapper::mapGovernmentEntityToAdminModel)
33-
.collect(Collectors.toList());
30+
public GovernmentTranslation findGovernmentById(Long id) {
31+
return governmentTranslationRepository.findGovernmentTranslationById(id)
32+
.orElseThrow(() -> {
33+
String message = String.format("Government with id: %s was not found", id);
34+
log.info(message);
35+
return new NoSuchElementException(message);
36+
});
3437
}
3538
}

government-service/src/main/java/com/csaba79coder/bestprotocol/model/representative/persistence/RepresentativeRepository.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@ public interface RepresentativeRepository extends JpaRepository<Representative,
1414
// Page<RepresentativeAdminModel> findByGovernmentId(@Param("government_id") Long governmentId, Pageable pageable);
1515
List<Representative> findRepresentativeByLanguageShortNameAndGovernmentId(@Param("language_short_name") String languageShortname, @Param("government_id") Long governmentId);
1616
List<Representative> findAllByLanguageShortName(String languageShortName);
17+
Representative findRepresentativeByLanguageShortName(String languageShortName);
1718
}

government-service/src/main/java/com/csaba79coder/bestprotocol/model/representative/service/RepresentativeService.java

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
package com.csaba79coder.bestprotocol.model.representative.service;
22

3+
import com.csaba79coder.bestprotocol.model.Availability;
34
import com.csaba79coder.bestprotocol.model.RepresentativeAdminModel;
45
import com.csaba79coder.bestprotocol.model.government.entity.Government;
56
import com.csaba79coder.bestprotocol.model.government.entity.GovernmentTranslation;
67
import com.csaba79coder.bestprotocol.model.government.persistence.GovernmentRepository;
78
import com.csaba79coder.bestprotocol.model.government.persistence.GovernmentTranslationRepository;
9+
import com.csaba79coder.bestprotocol.model.representative.entity.Representative;
810
import com.csaba79coder.bestprotocol.model.representative.persistence.RepresentativeRepository;
11+
import com.csaba79coder.bestprotocol.util.ImageUtil;
912
import com.csaba79coder.bestprotocol.util.mapper.Mapper;
1013
import lombok.RequiredArgsConstructor;
1114
import lombok.extern.slf4j.Slf4j;
@@ -14,6 +17,7 @@
1417

1518
import java.util.List;
1619
import java.util.NoSuchElementException;
20+
import java.util.Optional;
1721
import java.util.stream.Collectors;
1822

1923
@Service
@@ -32,8 +36,12 @@ public RepresentativeAdminModel addNewRepresentative(String languageShortName, S
3236
public List<RepresentativeAdminModel> renderAllRepresentatives(String languageShortName) {
3337
return representativeRepository.findAllByLanguageShortName(languageShortName)
3438
.stream()
35-
.map(Mapper::mapRepresentativeEntityToAdminModel)
39+
.map(representative -> getRepresentativeWithTranslation(languageShortName))
3640
.collect(Collectors.toList());
41+
/*return representativeRepository.findAllByLanguageShortName(languageShortName)
42+
.stream()
43+
.map(Mapper::mapRepresentativeEntityToAdminModel)
44+
.collect(Collectors.toList());*/
3745
}
3846

3947
public Government findGovernmentByName(String government) {
@@ -52,4 +60,27 @@ public List<RepresentativeAdminModel> renderAllRepresentativesByGovernmentId(Str
5260
.map(Mapper::mapRepresentativeEntityToAdminModel)
5361
.collect(Collectors.toList());
5462
}
63+
64+
private RepresentativeAdminModel getRepresentativeWithTranslation(String languageShortName) {
65+
Representative currentRepresentative = representativeRepository.findRepresentativeByLanguageShortName(languageShortName);
66+
Optional<GovernmentTranslation> governmentTranslation = governmentTranslationRepository.findGovernmentTranslationById(currentRepresentative.getGovernment().getId());
67+
governmentTranslation.ifPresent(translation -> currentRepresentative.setGovernment(translation.getGovernment()));
68+
return new RepresentativeAdminModel()
69+
.id(currentRepresentative.getId())
70+
.createdAt(String.valueOf(currentRepresentative.getCreatedAt()))
71+
.updatedAt(String.valueOf(currentRepresentative.getUpdatedAt()))
72+
.createdBy(currentRepresentative.getCreatedBy())
73+
.updatedBy(currentRepresentative.getUpdatedBy())
74+
.name(currentRepresentative.getName())
75+
.jobTitle(currentRepresentative.getJobTitle())
76+
.government(Mapper.mapGovernmentTranslationToAdminModel(governmentTranslation.get()))
77+
.secretairat(currentRepresentative.getSecretairat())
78+
.address(currentRepresentative.getAddress())
79+
.phoneNumber(currentRepresentative.getPhoneNumber())
80+
.email(currentRepresentative.getEmail())
81+
.image(ImageUtil.decompressImage(currentRepresentative.getImage()))
82+
// TODO add to api contract the secret note!
83+
.note(currentRepresentative.getNote())
84+
.availability(Availability.valueOf(currentRepresentative.getAvailability().name()));
85+
}
5586
}

government-service/src/main/java/com/csaba79coder/bestprotocol/util/mapper/Mapper.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22

33
import com.csaba79coder.bestprotocol.model.Availability;
44
import com.csaba79coder.bestprotocol.model.GovernmentAdminModel;
5+
import com.csaba79coder.bestprotocol.model.GovernmentTranslationModel;
56
import com.csaba79coder.bestprotocol.model.NewRepresentativeAdminModel;
67
import com.csaba79coder.bestprotocol.model.RepresentativeAdminModel;
78
import com.csaba79coder.bestprotocol.model.government.entity.Government;
9+
import com.csaba79coder.bestprotocol.model.government.entity.GovernmentTranslation;
810
import com.csaba79coder.bestprotocol.model.representative.entity.Representative;
911
import com.csaba79coder.bestprotocol.util.ImageUtil;
1012
import org.modelmapper.ModelMapper;
@@ -60,7 +62,7 @@ public static RepresentativeAdminModel mapRepresentativeEntityToAdminModel(Repre
6062
.updatedBy(entity.getUpdatedBy())
6163
.name(entity.getName())
6264
.jobTitle(entity.getJobTitle())
63-
.government(Mapper.mapGovernmentEntityToAdminModel(entity.getGovernment()))
65+
//.government(Mapper.mapGovernmentEntityToAdminModel(entity.getGovernment()))
6466
.secretairat(entity.getSecretairat())
6567
.address(entity.getAddress())
6668
.phoneNumber(entity.getPhoneNumber())
@@ -81,4 +83,17 @@ public static GovernmentAdminModel mapGovernmentEntityToAdminModel(Government en
8183
modelMapper.map(entity, model);
8284
return model;
8385
}
86+
87+
public static GovernmentAdminModel mapGovernmentTranslationToAdminModel(GovernmentTranslation translation) {
88+
GovernmentAdminModel model = new GovernmentAdminModel();
89+
modelMapper.map(translation, model);
90+
return model;
91+
}
92+
93+
public static GovernmentTranslationModel mapGovernmentTranslationToGovernmentAdminModel(GovernmentTranslation translation) {
94+
GovernmentTranslationModel model = new GovernmentTranslationModel();
95+
modelMapper.map(translation, model);
96+
return model;
97+
}
98+
8499
}

0 commit comments

Comments
 (0)