|
1
|
|
|
package com.osomapps.pt.reportphoto; |
|
2
|
|
|
|
|
3
|
|
|
import com.osomapps.pt.UnauthorizedException; |
|
4
|
|
|
import com.osomapps.pt.goals.InUserPhotoRepository; |
|
5
|
|
|
import com.osomapps.pt.token.InUserLogin; |
|
6
|
|
|
import com.osomapps.pt.token.InUserPhoto; |
|
7
|
|
|
import com.osomapps.pt.tokenemail.DataurlValidator; |
|
8
|
|
|
import com.osomapps.pt.user.UserService; |
|
9
|
|
|
import java.time.LocalDateTime; |
|
10
|
|
|
import java.util.Arrays; |
|
11
|
|
|
import java.util.Collections; |
|
12
|
|
|
import java.util.HashMap; |
|
13
|
|
|
import java.util.List; |
|
14
|
|
|
import java.util.stream.Collectors; |
|
15
|
|
|
import org.springframework.stereotype.Service; |
|
16
|
|
|
import org.springframework.validation.MapBindingResult; |
|
17
|
|
|
|
|
18
|
|
|
@Service |
|
19
|
|
|
class ReportPhotoService { |
|
20
|
|
|
private final InUserPhotoRepository inUserPhotoRepository; |
|
21
|
|
|
private final UserService userService; |
|
22
|
|
|
private final DataurlValidator dataurlValidator; |
|
23
|
|
|
|
|
24
|
|
|
ReportPhotoService( |
|
25
|
|
|
InUserPhotoRepository inUserPhotoRepository, |
|
26
|
|
|
UserService userService, |
|
27
|
|
|
DataurlValidator dataurlValidator) { |
|
28
|
|
|
this.inUserPhotoRepository = inUserPhotoRepository; |
|
29
|
|
|
this.userService = userService; |
|
30
|
|
|
this.dataurlValidator = dataurlValidator; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
List<PhotoResponseDTO> findAll(String token) { |
|
34
|
|
|
if (!token.isEmpty()) { |
|
35
|
|
|
final InUserLogin inUserLogin = userService.checkUserToken(token); |
|
36
|
|
|
return inUserLogin.getInUser().getInUserPhotos().stream() |
|
37
|
|
|
.map( |
|
38
|
|
|
inUserPhoto -> |
|
39
|
|
|
new PhotoResponseDTO() |
|
40
|
|
|
.setDate(inUserPhoto.getCreated()) |
|
41
|
|
|
.setPhoto( |
|
42
|
|
|
"/api/v1/report-photo-file/" |
|
43
|
|
|
+ inUserPhoto.getId() |
|
44
|
|
|
+ "/" |
|
45
|
|
|
+ inUserPhoto.getFile_name())) |
|
46
|
|
|
.collect(Collectors.toList()); |
|
47
|
|
|
} |
|
48
|
|
|
return Collections.emptyList(); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
PhotoResponseDTO create(String token, PhotoRequestDTO photoRequestDTO) { |
|
52
|
|
|
if (!token.isEmpty()) { |
|
53
|
|
|
final InUserLogin inUserLogin = userService.checkUserToken(token); |
|
54
|
|
|
final MapBindingResult errors = |
|
55
|
|
|
new MapBindingResult(new HashMap<>(), String.class.getName()); |
|
56
|
|
|
dataurlValidator.validate(photoRequestDTO.getDataurl(), errors); |
|
57
|
|
|
if (errors.hasErrors()) { |
|
58
|
|
|
throw new UnauthorizedException(errors.getAllErrors().get(0).getDefaultMessage()); |
|
59
|
|
|
} |
|
60
|
|
|
final String fileType = extractFileType(photoRequestDTO.getDataurl()); |
|
61
|
|
|
final InUserPhoto inUserPhoto = |
|
62
|
|
|
new InUserPhoto() |
|
63
|
|
|
.setGoal_id(photoRequestDTO.getGoal_id()) |
|
64
|
|
|
.setInUsers(Arrays.asList(inUserLogin.getInUser())) |
|
65
|
|
|
.setData_url(photoRequestDTO.getDataurl()) |
|
66
|
|
|
.setFile_type(fileType) |
|
67
|
|
|
.setFile_name(extractFileName(fileType)); |
|
68
|
|
|
final InUserPhoto inUserPhotoSaved = inUserPhotoRepository.save(inUserPhoto); |
|
69
|
|
|
return new PhotoResponseDTO() |
|
70
|
|
|
.setDate(LocalDateTime.now()) |
|
71
|
|
|
.setPhoto( |
|
72
|
|
|
"/api/v1/report-photo-file/" |
|
73
|
|
|
+ inUserPhotoSaved.getId() |
|
74
|
|
|
+ "/" |
|
75
|
|
|
+ inUserPhoto.getFile_name()); |
|
76
|
|
|
} |
|
77
|
|
|
return new PhotoResponseDTO(); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
private String extractFileType(String dataurl) { |
|
81
|
|
|
return dataurl.substring(5, Math.max(5, dataurl.indexOf(";"))); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
private String extractFileName(String fileType) { |
|
85
|
|
|
switch (fileType) { |
|
86
|
|
|
case "image/png": |
|
87
|
|
|
return "photo.png"; |
|
88
|
|
|
case "image/jpeg": |
|
89
|
|
|
default: |
|
90
|
|
|
return "photo.jpg"; |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|