1
|
|
|
package com.osomapps.pt.reportweight; |
2
|
|
|
|
3
|
|
|
import com.osomapps.pt.token.InUserLogin; |
4
|
|
|
import com.osomapps.pt.user.UserService; |
5
|
|
|
import java.util.Collections; |
6
|
|
|
import java.util.List; |
7
|
|
|
import java.util.stream.Collectors; |
8
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
9
|
|
|
import org.springframework.stereotype.Service; |
10
|
|
|
|
11
|
|
|
@Service |
12
|
|
|
class ReportWeightService { |
13
|
|
|
private final InUserWeightRepository inUserWeightRepository; |
14
|
|
|
private final UserService userService; |
15
|
|
|
|
16
|
|
|
@Autowired |
17
|
|
|
ReportWeightService(InUserWeightRepository inUserWeightRepository, UserService userService) { |
18
|
|
|
this.inUserWeightRepository = inUserWeightRepository; |
19
|
|
|
this.userService = userService; |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
List<WeightResponseDTO> findAll(String token) { |
23
|
|
|
if (!token.isEmpty()) { |
24
|
|
|
final InUserLogin inUserLogin = userService.checkUserToken(token); |
25
|
|
|
return inUserLogin.getInUser().getInUserWeights().stream() |
26
|
|
|
.map( |
27
|
|
|
inUserWeight -> |
28
|
|
|
new WeightResponseDTO( |
29
|
|
|
inUserWeight.getId(), |
30
|
|
|
inUserWeight.getWeight().longValue())) |
31
|
|
|
.collect(Collectors.toList()); |
32
|
|
|
} |
33
|
|
|
return Collections.emptyList(); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
WeightResponseDTO create(String token, WeightRequestDTO weightRequestDTO) { |
37
|
|
|
if (!token.isEmpty()) { |
38
|
|
|
final InUserLogin inUserLogin = userService.checkUserToken(token); |
39
|
|
|
InUserWeight inUserWeight = new InUserWeight(); |
40
|
|
|
inUserWeight.setInUser(inUserLogin.getInUser()); |
41
|
|
|
inUserWeight.setWeight(weightRequestDTO.getWeight().floatValue()); |
42
|
|
|
InUserWeight InUserWeightSaved = inUserWeightRepository.save(inUserWeight); |
43
|
|
|
return new WeightResponseDTO( |
44
|
|
|
InUserWeightSaved.getId(), InUserWeightSaved.getWeight().longValue()); |
45
|
|
|
} |
46
|
|
|
return new WeightResponseDTO(); |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
|