Skip to content

Commit 7df24f2

Browse files
Refactor fillCustomers for CustomerController
1 parent d827923 commit 7df24f2

File tree

1 file changed

+33
-29
lines changed

1 file changed

+33
-29
lines changed

src/main/java/main/controllers/CustomerController.java

Lines changed: 33 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -131,51 +131,55 @@ public boolean delete(CustomerAttachmentDto template) throws RPException {
131131
}
132132
}
133133

134-
//TODO Refactoring
135134
private List<CustomerDto> fillCustomers(List<CustomerDto> customers) throws RPException {
136-
UserDto userTemplate = new UserDto();
137-
userTemplate.setUnit_coordinator(1);
138-
List<UserDto> coordinators = userDao.searchAll(userTemplate);
139-
userTemplate.setUnit_coordinator(null);
140-
userTemplate.setAccount_manager(1);
141-
List<UserDto> accountManagers = userDao.searchAll(userTemplate);
135+
List<UserDto> users = userDao.getAll();
142136

143137
for (CustomerDto customer: customers){
144-
customer.setCoordinator(coordinators.stream().filter(x -> x.getId().equals(customer.getCoordinator_id())).findFirst().orElse(null));
138+
customer.setCoordinator(users.stream().filter(x -> x.getId().equals(customer.getCoordinator_id())).findFirst().orElse(null));
145139
customer.getCoordinator().toPublic();
140+
146141
if(customer.getAccounting() == 1){
147142
Integer accountManagerId = customer.getAccount_manager_id();
148-
customer.setAccount_manager(accountManagers.stream().filter(x->x.getId().equals(accountManagerId)).findFirst().orElse(null));
143+
customer.setAccount_manager(users.stream().filter(x->x.getId().equals(accountManagerId)).findFirst().orElse(null));
149144
customer.getAccount_manager().toPublic();
150145
}
151146

152147
if(baseUser.isCoordinator()){
153-
CustomerMemberDto customerMemberDto = new CustomerMemberDto();
154-
customerMemberDto.setCustomer_id(customer.getId());
155-
List<CustomerMemberDto> customerMembers = get(customerMemberDto);
156-
customer.setAccount_team(customerMembers);
157-
158-
CustomerCommentDto customerCommentDtoTemplate = new CustomerCommentDto();
159-
customerCommentDtoTemplate.setCustomer_id(customer.getId());
160-
List<CustomerCommentDto> customerComments = get(customerCommentDtoTemplate);
161-
customer.setComments(customerComments);
162-
163-
164-
CustomerAttachmentDto customerAttachmentDtoTemplate = new CustomerAttachmentDto();
165-
customerAttachmentDtoTemplate.setCustomer_id(customer.getId());
166-
List<CustomerAttachmentDto> customerAttachments = get(customerAttachmentDtoTemplate);
167-
customer.setAttachments(customerAttachments);
168-
169-
ProjectDao projectDao = new ProjectDao();
170-
ProjectDto projectTemplate = new ProjectDto();
171-
projectTemplate.setCustomer_id(customer.getId());
172-
customer.setProjects(projectDao.searchAll(projectTemplate));
148+
customer.setAccount_team(getMembers(customer));
149+
customer.setComments(getComments(customer));
150+
customer.setAttachments(getAttachments(customer));
151+
customer.setProjects(getProjects(customer));
173152
}
174153
}
175154

176155
return customers;
177156
}
178157

158+
private List<ProjectDto> getProjects(CustomerDto customer) throws RPException {
159+
ProjectDao projectDao = new ProjectDao();
160+
ProjectDto projectTemplate = new ProjectDto();
161+
projectTemplate.setCustomer_id(customer.getId());
162+
return projectDao.searchAll(projectTemplate);
163+
}
164+
165+
private List<CustomerAttachmentDto> getAttachments(CustomerDto customer) throws RPException {
166+
CustomerAttachmentDto customerAttachmentDtoTemplate = new CustomerAttachmentDto();
167+
customerAttachmentDtoTemplate.setCustomer_id(customer.getId());
168+
return get(customerAttachmentDtoTemplate);
169+
}
170+
171+
private List<CustomerCommentDto> getComments(CustomerDto customer) throws RPException {
172+
CustomerCommentDto customerCommentDtoTemplate = new CustomerCommentDto();
173+
customerCommentDtoTemplate.setCustomer_id(customer.getId());
174+
return get(customerCommentDtoTemplate);
175+
}
176+
177+
private List<CustomerMemberDto> getMembers(CustomerDto customer) throws RPException {
178+
CustomerMemberDto customerMemberDto = new CustomerMemberDto();
179+
customerMemberDto.setCustomer_id(customer.getId());
180+
return get(customerMemberDto);
181+
}
182+
179183
private CustomerDto fillCustomer(CustomerDto customer) throws RPException {
180184
List<CustomerDto> customers = new ArrayList<>();
181185
customers.add(customer);

0 commit comments

Comments
 (0)