Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
B
baby-monitoring-spring-backend
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
R24-145
baby-monitoring-spring-backend
Commits
045a2635
Commit
045a2635
authored
Oct 19, 2024
by
Ishankha K.C
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'feature/chamode_dev' into 'master'
db host change and document management apis See merge request
!5
parents
b8dd6ac9
f8379cc4
Changes
20
Show whitespace changes
Inline
Side-by-side
Showing
20 changed files
with
276 additions
and
105 deletions
+276
-105
src/main/java/com/kaluwa/enterprises/babycarebackendservice/constants/DocumentTypes.java
...rises/babycarebackendservice/constants/DocumentTypes.java
+2
-1
src/main/java/com/kaluwa/enterprises/babycarebackendservice/dao/DocumentDao.java
...a/enterprises/babycarebackendservice/dao/DocumentDao.java
+3
-1
src/main/java/com/kaluwa/enterprises/babycarebackendservice/dao/UserDao.java
...aluwa/enterprises/babycarebackendservice/dao/UserDao.java
+4
-0
src/main/java/com/kaluwa/enterprises/babycarebackendservice/dto/AuthenticationDto.java
...rprises/babycarebackendservice/dto/AuthenticationDto.java
+1
-0
src/main/java/com/kaluwa/enterprises/babycarebackendservice/dto/BabyDto.java
...aluwa/enterprises/babycarebackendservice/dto/BabyDto.java
+1
-0
src/main/java/com/kaluwa/enterprises/babycarebackendservice/dto/DocumentDto.java
...a/enterprises/babycarebackendservice/dto/DocumentDto.java
+2
-3
src/main/java/com/kaluwa/enterprises/babycarebackendservice/dto/UserDto.java
...aluwa/enterprises/babycarebackendservice/dto/UserDto.java
+1
-0
src/main/java/com/kaluwa/enterprises/babycarebackendservice/mappers/DocumentMapper.java
...prises/babycarebackendservice/mappers/DocumentMapper.java
+0
-9
src/main/java/com/kaluwa/enterprises/babycarebackendservice/model/Baby.java
...kaluwa/enterprises/babycarebackendservice/model/Baby.java
+1
-0
src/main/java/com/kaluwa/enterprises/babycarebackendservice/model/Document.java
...wa/enterprises/babycarebackendservice/model/Document.java
+5
-3
src/main/java/com/kaluwa/enterprises/babycarebackendservice/model/User.java
...kaluwa/enterprises/babycarebackendservice/model/User.java
+1
-0
src/main/java/com/kaluwa/enterprises/babycarebackendservice/rest/DocumentController.java
...rises/babycarebackendservice/rest/DocumentController.java
+42
-0
src/main/java/com/kaluwa/enterprises/babycarebackendservice/rest/UserController.java
...terprises/babycarebackendservice/rest/UserController.java
+11
-11
src/main/java/com/kaluwa/enterprises/babycarebackendservice/service/DocumentService.java
...rises/babycarebackendservice/service/DocumentService.java
+11
-0
src/main/java/com/kaluwa/enterprises/babycarebackendservice/service/UserService.java
...terprises/babycarebackendservice/service/UserService.java
+3
-3
src/main/java/com/kaluwa/enterprises/babycarebackendservice/service/impl/BabyServiceImpl.java
.../babycarebackendservice/service/impl/BabyServiceImpl.java
+3
-0
src/main/java/com/kaluwa/enterprises/babycarebackendservice/service/impl/DocumentServiceImpl.java
...ycarebackendservice/service/impl/DocumentServiceImpl.java
+110
-0
src/main/java/com/kaluwa/enterprises/babycarebackendservice/service/impl/UserServiceImpl.java
.../babycarebackendservice/service/impl/UserServiceImpl.java
+68
-72
src/main/java/com/kaluwa/enterprises/babycarebackendservice/utils/Utils.java
...aluwa/enterprises/babycarebackendservice/utils/Utils.java
+6
-1
src/main/resources/application.yaml
src/main/resources/application.yaml
+1
-1
No files found.
src/main/java/com/kaluwa/enterprises/babycarebackendservice/constants/DocumetTypes.java
→
src/main/java/com/kaluwa/enterprises/babycarebackendservice/constants/Docume
n
tTypes.java
View file @
045a2635
package
com.kaluwa.enterprises.babycarebackendservice.constants
;
public
class
DocumetTypes
{
public
class
Docume
n
tTypes
{
public
static
final
String
PROFILE_IMAGE
=
"profile_image"
;
public
static
final
String
BABY_PROFILE_IMAGE
=
"baby_profile_image"
;
public
static
final
String
IMAGE
=
"image"
;
}
src/main/java/com/kaluwa/enterprises/babycarebackendservice/dao/DocumentDao.java
View file @
045a2635
...
...
@@ -9,5 +9,7 @@ import java.util.Optional;
@Repository
public
interface
DocumentDao
extends
JpaRepository
<
Document
,
Long
>
{
Optional
<
Document
>
findByUserUserIdAndDocumentType
(
Long
userId
,
String
profileImage
);
// Optional<Document> findByUserUserIdAndDocumentType(Long userId, String profileImage);
Optional
<
Document
>
findByTableNameAndUniqKeyAndDocumentType
(
String
table
,
String
uniqKey
,
String
documentType
);
}
\ No newline at end of file
src/main/java/com/kaluwa/enterprises/babycarebackendservice/dao/UserDao.java
View file @
045a2635
package
com.kaluwa.enterprises.babycarebackendservice.dao
;
import
com.kaluwa.enterprises.babycarebackendservice.model.User
;
import
jakarta.validation.constraints.NotEmpty
;
import
jakarta.validation.constraints.NotNull
;
import
org.springframework.data.jpa.repository.JpaRepository
;
import
org.springframework.stereotype.Repository
;
...
...
@@ -11,4 +13,6 @@ public interface UserDao extends JpaRepository<User, Long> {
Optional
<
User
>
findByEmail
(
String
username
);
boolean
existsByEmail
(
String
email
);
boolean
existsByUniqKey
(
String
uniqKey
);
}
src/main/java/com/kaluwa/enterprises/babycarebackendservice/dto/AuthenticationDto.java
View file @
045a2635
...
...
@@ -15,5 +15,6 @@ public class AuthenticationDto {
private
String
phone
;
private
String
role
;
private
String
status
;
private
String
uniqKey
;
private
TokenDto
tokenDto
;
}
src/main/java/com/kaluwa/enterprises/babycarebackendservice/dto/BabyDto.java
View file @
045a2635
...
...
@@ -52,6 +52,7 @@ public class BabyDto {
private
String
secondaryEmergencyRelationship
;
private
String
secondaryEmergencyContactNumber
;
private
String
notes
;
private
String
uniqKey
;
private
UserDto
user
;
@NotNull
(
message
=
"Logged user id is required"
)
...
...
src/main/java/com/kaluwa/enterprises/babycarebackendservice/dto/DocumentDto.java
View file @
045a2635
...
...
@@ -17,8 +17,7 @@ public class DocumentDto implements Serializable {
private
Long
documentId
;
private
String
documentType
;
private
String
documentName
;
private
String
tableName
;
private
String
uniqKey
;
private
byte
[]
data
;
private
UserDto
user
;
private
Long
userId
;
}
\ No newline at end of file
src/main/java/com/kaluwa/enterprises/babycarebackendservice/dto/UserDto.java
View file @
045a2635
...
...
@@ -18,5 +18,6 @@ public class UserDto {
private
String
phone
;
private
String
role
;
private
String
status
;
private
String
uniqKey
;
private
LocalDate
dob
;
}
src/main/java/com/kaluwa/enterprises/babycarebackendservice/mappers/DocumentMapper.java
View file @
045a2635
...
...
@@ -11,19 +11,10 @@ import java.util.List;
@Mapper
(
componentModel
=
"spring"
)
public
interface
DocumentMapper
{
@Mappings
({
@Mapping
(
target
=
"userId"
,
source
=
"user.userId"
)
})
DocumentDto
toDto
(
Document
document
);
@Mappings
({
@Mapping
(
target
=
"user.userId"
,
source
=
"userId"
)
})
Document
toEntity
(
DocumentDto
documentDto
);
@Mappings
({
@Mapping
(
target
=
"userId"
,
source
=
"user.userId"
)
})
List
<
DocumentDto
>
listToDto
(
List
<
Document
>
documents
);
}
src/main/java/com/kaluwa/enterprises/babycarebackendservice/model/Baby.java
View file @
045a2635
...
...
@@ -49,6 +49,7 @@ public class Baby {
private
String
secondaryEmergencyRelationship
;
private
String
secondaryEmergencyContactNumber
;
private
String
notes
;
private
String
uniqKey
;
@ManyToOne
(
fetch
=
FetchType
.
LAZY
)
@JoinColumn
(
name
=
"userId"
,
referencedColumnName
=
"userId"
,
nullable
=
false
)
private
User
user
;
...
...
src/main/java/com/kaluwa/enterprises/babycarebackendservice/model/Document.java
View file @
045a2635
...
...
@@ -19,10 +19,12 @@ public class Document {
private
Long
documentId
;
private
String
documentType
;
private
String
documentName
;
private
String
tableName
;
private
String
uniqKey
;
@Lob
@Column
(
name
=
"data"
,
columnDefinition
=
"LONGBLOB"
)
private
byte
[]
data
;
@ManyToOne
(
fetch
=
FetchType
.
LAZY
)
@JoinColumn
(
name
=
"userId"
,
referencedColumnName
=
"userId"
,
nullable
=
false
)
private
User
user
;
//
@ManyToOne(fetch = FetchType.LAZY)
//
@JoinColumn(name = "userId", referencedColumnName = "userId", nullable = false)
//
private User user;
}
src/main/java/com/kaluwa/enterprises/babycarebackendservice/model/User.java
View file @
045a2635
...
...
@@ -30,6 +30,7 @@ public class User implements UserDetails {
private
String
phone
;
private
String
role
;
private
String
status
;
private
String
uniqKey
;
private
LocalDate
dob
;
@Override
...
...
src/main/java/com/kaluwa/enterprises/babycarebackendservice/rest/DocumentController.java
0 → 100644
View file @
045a2635
package
com.kaluwa.enterprises.babycarebackendservice.rest
;
import
com.kaluwa.enterprises.babycarebackendservice.dto.ResponseDto
;
import
com.kaluwa.enterprises.babycarebackendservice.service.DocumentService
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.http.HttpStatus
;
import
org.springframework.http.ResponseEntity
;
import
org.springframework.web.bind.annotation.*
;
import
org.springframework.web.multipart.MultipartFile
;
@RestController
@RequestMapping
(
"/document"
)
@Slf4j
public
class
DocumentController
{
private
final
DocumentService
documentService
;
public
DocumentController
(
DocumentService
documentService
)
{
this
.
documentService
=
documentService
;
}
@PutMapping
(
"/{docType}/{table}/{uniqKey}"
)
public
ResponseEntity
<
ResponseDto
>
uploadImage
(
@PathVariable
String
docType
,
@PathVariable
String
table
,
@PathVariable
String
uniqKey
,
@RequestParam
(
"image"
)
MultipartFile
file
)
{
log
.
info
(
"Inside document controller upload image method"
);
return
documentService
.
uploadImage
(
docType
,
table
,
uniqKey
,
file
);
}
@GetMapping
(
"/{table}/{uniqKey}"
)
public
ResponseEntity
<
byte
[]>
getImage
(
@PathVariable
String
table
,
@PathVariable
String
uniqKey
)
{
log
.
info
(
"Inside document controller get image method"
);
return
documentService
.
getImage
(
table
,
uniqKey
);
}
}
src/main/java/com/kaluwa/enterprises/babycarebackendservice/rest/UserController.java
View file @
045a2635
...
...
@@ -49,15 +49,15 @@ public class UserController {
return
new
ResponseEntity
<>(
userService
.
deleteUser
(
userId
),
HttpStatus
.
OK
);
}
@PutMapping
(
"/image/{userId}"
)
public
ResponseEntity
<
ResponseDto
>
uploadImage
(
@PathVariable
Long
userId
,
@RequestParam
(
"image"
)
MultipartFile
file
)
{
log
.
info
(
"Inside user controller upload image method"
);
return
new
ResponseEntity
<>(
userService
.
uploadImage
(
userId
,
file
),
HttpStatus
.
OK
);
}
@GetMapping
(
"/image/{userId}"
)
public
ResponseEntity
<
byte
[]>
getImage
(
@PathVariable
Long
userId
)
{
log
.
info
(
"Inside user controller get image method"
);
return
userService
.
getImage
(
userId
);
}
//
@PutMapping("/image/{userId}")
//
public ResponseEntity<ResponseDto> uploadImage(@PathVariable Long userId, @RequestParam("image") MultipartFile file) {
//
log.info("Inside user controller upload image method");
//
return new ResponseEntity<>(userService.uploadImage(userId, file), HttpStatus.OK);
//
}
//
//
@GetMapping("/image/{userId}")
//
public ResponseEntity<byte[]> getImage(@PathVariable Long userId) {
//
log.info("Inside user controller get image method");
//
return userService.getImage(userId);
//
}
}
src/main/java/com/kaluwa/enterprises/babycarebackendservice/service/DocumentService.java
0 → 100644
View file @
045a2635
package
com.kaluwa.enterprises.babycarebackendservice.service
;
import
com.kaluwa.enterprises.babycarebackendservice.dto.ResponseDto
;
import
org.springframework.http.ResponseEntity
;
import
org.springframework.web.multipart.MultipartFile
;
public
interface
DocumentService
{
ResponseEntity
<
ResponseDto
>
uploadImage
(
String
docType
,
String
table
,
String
uniqKey
,
MultipartFile
file
);
ResponseEntity
<
byte
[]>
getImage
(
String
table
,
String
uniqKey
);
}
src/main/java/com/kaluwa/enterprises/babycarebackendservice/service/UserService.java
View file @
045a2635
...
...
@@ -19,7 +19,7 @@ public interface UserService {
ResponseDto
deleteUser
(
Long
userId
);
ResponseDto
uploadImage
(
Long
userId
,
MultipartFile
file
);
ResponseEntity
<
byte
[]>
getImage
(
Long
userId
);
//
ResponseDto uploadImage(Long userId, MultipartFile file);
//
//
ResponseEntity<byte[]> getImage(Long userId);
}
src/main/java/com/kaluwa/enterprises/babycarebackendservice/service/impl/BabyServiceImpl.java
View file @
045a2635
...
...
@@ -17,6 +17,8 @@ import org.springframework.stereotype.Service;
import
java.util.List
;
import
java.util.Optional
;
import
static
com
.
kaluwa
.
enterprises
.
babycarebackendservice
.
utils
.
Utils
.
uniqKeyGenerator
;
@Service
@Slf4j
public
class
BabyServiceImpl
implements
BabyService
{
...
...
@@ -35,6 +37,7 @@ public class BabyServiceImpl implements BabyService {
public
BabyDto
createBaby
(
BabyDto
babyDto
)
{
log
.
info
(
"Inside baby service createBaby method"
);
try
{
babyDto
.
setUniqKey
(
uniqKeyGenerator
());
return
babyMapper
.
toDto
(
babyDao
.
save
(
babyMapper
.
toEntity
(
babyDto
)));
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
...
...
src/main/java/com/kaluwa/enterprises/babycarebackendservice/service/impl/DocumentServiceImpl.java
0 → 100644
View file @
045a2635
package
com.kaluwa.enterprises.babycarebackendservice.service.impl
;
import
com.kaluwa.enterprises.babycarebackendservice.dao.DocumentDao
;
import
com.kaluwa.enterprises.babycarebackendservice.dto.DocumentDto
;
import
com.kaluwa.enterprises.babycarebackendservice.dto.ResponseDto
;
import
com.kaluwa.enterprises.babycarebackendservice.error.BadRequestAlertException
;
import
com.kaluwa.enterprises.babycarebackendservice.mappers.DocumentMapper
;
import
com.kaluwa.enterprises.babycarebackendservice.model.Document
;
import
com.kaluwa.enterprises.babycarebackendservice.service.DocumentService
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.http.HttpStatus
;
import
org.springframework.http.MediaType
;
import
org.springframework.http.ResponseEntity
;
import
org.springframework.jdbc.core.JdbcTemplate
;
import
org.springframework.stereotype.Service
;
import
org.springframework.web.multipart.MultipartFile
;
import
java.util.Optional
;
import
static
com
.
kaluwa
.
enterprises
.
babycarebackendservice
.
constants
.
DocumentTypes
.
IMAGE
;
import
static
com
.
kaluwa
.
enterprises
.
babycarebackendservice
.
utils
.
Utils
.
ALLOWED_CONTENT_TYPES
;
import
static
com
.
kaluwa
.
enterprises
.
babycarebackendservice
.
utils
.
Utils
.
determineImageContentType
;
@Service
@Slf4j
public
class
DocumentServiceImpl
implements
DocumentService
{
private
final
DocumentDao
documentDao
;
private
final
DocumentMapper
documentMapper
;
private
final
JdbcTemplate
jdbcTemplate
;
public
DocumentServiceImpl
(
DocumentDao
documentDao
,
DocumentMapper
documentMapper
,
JdbcTemplate
jdbcTemplate
)
{
this
.
documentDao
=
documentDao
;
this
.
documentMapper
=
documentMapper
;
this
.
jdbcTemplate
=
jdbcTemplate
;
}
@Override
public
ResponseEntity
<
ResponseDto
>
uploadImage
(
String
docType
,
String
table
,
String
uniqKey
,
MultipartFile
file
)
{
log
.
info
(
"Inside document service upload image method"
);
try
{
if
(!
recordExists
(
table
,
uniqKey
))
{
throw
new
BadRequestAlertException
(
"Record not found by uniqKey: "
+
uniqKey
+
" on "
+
table
+
" table."
,
"Error"
,
"Record not found"
);
}
else
{
Optional
<
Document
>
documentOp
=
documentDao
.
findByTableNameAndUniqKeyAndDocumentType
(
table
,
uniqKey
,
IMAGE
);
DocumentDto
documentDto
=
new
DocumentDto
();
if
(
documentOp
.
isPresent
())
{
documentDto
=
documentMapper
.
toDto
(
documentOp
.
get
());
}
// save file into system
if
(
file
.
isEmpty
())
{
throw
new
BadRequestAlertException
(
"Please select a Image to upload"
,
"User"
,
"Image upload failed"
);
}
// Validate file content type
String
contentType
=
file
.
getContentType
();
if
(!
ALLOWED_CONTENT_TYPES
.
contains
(
contentType
))
{
throw
new
BadRequestAlertException
(
"Invalid file type. Only JPEG, PNG, and JPG files are allowed."
,
"User"
,
"Image upload failed"
);
}
documentDto
.
setDocumentType
(
IMAGE
);
documentDto
.
setDocumentName
(
file
.
getOriginalFilename
());
documentDto
.
setData
(
file
.
getBytes
());
documentDto
.
setTableName
(
table
);
documentDto
.
setUniqKey
(
uniqKey
);
// save image
documentDto
=
documentMapper
.
toDto
(
documentDao
.
save
(
documentMapper
.
toEntity
(
documentDto
)));
// response
ResponseDto
responseDto
=
new
ResponseDto
();
responseDto
.
setId
(
documentDto
.
getDocumentId
());
responseDto
.
setMessage
(
"Image uploaded successfully"
);
log
.
info
(
"Image uploaded successfully"
);
return
new
ResponseEntity
<>(
responseDto
,
HttpStatus
.
OK
);
}
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
log
.
error
(
"Error occurred while uploading image: "
+
e
.
getMessage
());
throw
new
BadRequestAlertException
(
e
.
getMessage
(),
"User"
,
"error"
);
}
}
@Override
public
ResponseEntity
<
byte
[]>
getImage
(
String
table
,
String
uniqKey
)
{
log
.
info
(
"Inside document service get image method"
);
try
{
if
(!
recordExists
(
table
,
uniqKey
))
{
throw
new
BadRequestAlertException
(
"Record not found by uniqKey: "
+
uniqKey
+
" on "
+
table
+
" table."
,
"Error"
,
"Record not found"
);
}
else
{
Optional
<
Document
>
documentOp
=
documentDao
.
findByTableNameAndUniqKeyAndDocumentType
(
table
,
uniqKey
,
IMAGE
);
if
(
documentOp
.
isEmpty
())
{
throw
new
BadRequestAlertException
(
"Image not found"
,
"User"
,
"Image not found"
);
}
else
{
DocumentDto
documentDto
=
documentMapper
.
toDto
(
documentOp
.
get
());
return
ResponseEntity
.
ok
()
.
contentType
(
MediaType
.
valueOf
(
determineImageContentType
(
documentDto
.
getDocumentName
())))
.
body
(
documentDto
.
getData
());
}
}
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
throw
new
BadRequestAlertException
(
e
.
getMessage
(),
"User"
,
"error"
);
}
}
private
boolean
recordExists
(
String
tableName
,
String
uniqueKey
)
{
String
query
=
"SELECT COUNT(*) FROM "
+
tableName
+
" WHERE uniq_key = ?"
;
Integer
count
=
jdbcTemplate
.
queryForObject
(
query
,
new
Object
[]{
uniqueKey
},
Integer
.
class
);
return
count
!=
null
&&
count
>
0
;
}
}
src/main/java/com/kaluwa/enterprises/babycarebackendservice/service/impl/UserServiceImpl.java
View file @
045a2635
...
...
@@ -6,30 +6,24 @@ import com.kaluwa.enterprises.babycarebackendservice.dto.*;
import
com.kaluwa.enterprises.babycarebackendservice.error.BadRequestAlertException
;
import
com.kaluwa.enterprises.babycarebackendservice.mappers.DocumentMapper
;
import
com.kaluwa.enterprises.babycarebackendservice.mappers.UserMapper
;
import
com.kaluwa.enterprises.babycarebackendservice.model.Document
;
import
com.kaluwa.enterprises.babycarebackendservice.model.User
;
import
com.kaluwa.enterprises.babycarebackendservice.service.UserService
;
import
com.kaluwa.enterprises.babycarebackendservice.utils.JWTUtils
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.http.MediaType
;
import
org.springframework.http.ResponseEntity
;
import
org.springframework.security.authentication.AuthenticationManager
;
import
org.springframework.security.authentication.UsernamePasswordAuthenticationToken
;
import
org.springframework.security.crypto.password.PasswordEncoder
;
import
org.springframework.stereotype.Service
;
import
org.springframework.web.multipart.MultipartFile
;
import
java.util.HashMap
;
import
java.util.List
;
import
java.util.Optional
;
import
static
com
.
kaluwa
.
enterprises
.
babycarebackendservice
.
constants
.
Common
.
TOKEN_TYPE
;
import
static
com
.
kaluwa
.
enterprises
.
babycarebackendservice
.
constants
.
DocumetTypes
.
PROFILE_IMAGE
;
import
static
com
.
kaluwa
.
enterprises
.
babycarebackendservice
.
constants
.
Status
.
STATUS_NEW
;
import
static
com
.
kaluwa
.
enterprises
.
babycarebackendservice
.
constants
.
UserRoles
.
USER
;
import
static
com
.
kaluwa
.
enterprises
.
babycarebackendservice
.
utils
.
JWTUtils
.
EXPIRATION_TIME
;
import
static
com
.
kaluwa
.
enterprises
.
babycarebackendservice
.
utils
.
Utils
.
ALLOWED_CONTENT_TYPES
;
import
static
com
.
kaluwa
.
enterprises
.
babycarebackendservice
.
utils
.
Utils
.
determineImageContentType
;
import
static
com
.
kaluwa
.
enterprises
.
babycarebackendservice
.
utils
.
Utils
.
uniqKeyGenerator
;
@Service
@Slf4j
...
...
@@ -73,6 +67,7 @@ public class UserServiceImpl implements UserService {
authenticationDto
.
setPhone
(
user
.
getPhone
());
authenticationDto
.
setRole
(
user
.
getRole
());
authenticationDto
.
setStatus
(
user
.
getStatus
());
authenticationDto
.
setUniqKey
(
user
.
getUniqKey
());
TokenDto
tokenDto
=
new
TokenDto
();
tokenDto
.
setTokenType
(
TOKEN_TYPE
);
...
...
@@ -105,6 +100,7 @@ public class UserServiceImpl implements UserService {
userDto
.
setPhone
(
registerRequest
.
getPhone
());
userDto
.
setRole
(
USER
);
userDto
.
setStatus
(
STATUS_NEW
);
userDto
.
setUniqKey
(
uniqKeyGenerator
());
User
user
=
userMapper
.
toEntity
(
userDto
);
user
.
setPassword
(
passwordEncoder
.
encode
(
registerRequest
.
getPassword
()));
...
...
@@ -196,69 +192,69 @@ public class UserServiceImpl implements UserService {
}
}
@Override
public
ResponseDto
uploadImage
(
Long
userId
,
MultipartFile
file
)
{
log
.
info
(
"Inside user service upload image method"
);
try
{
Optional
<
User
>
userOp
=
userDao
.
findById
(
userId
);
if
(
userOp
.
isEmpty
())
{
throw
new
BadRequestAlertException
(
"User not found"
,
"User"
,
"User not found"
);
}
else
{
Optional
<
Document
>
documentOp
=
documentDao
.
findByUserUserIdAndDocumentType
(
userId
,
PROFILE_IMAGE
);
DocumentDto
documentDto
=
new
DocumentDto
();
if
(
documentOp
.
isPresent
())
{
documentDto
=
documentMapper
.
toDto
(
documentOp
.
get
());
}
// save file into system
if
(
file
.
isEmpty
())
{
throw
new
BadRequestAlertException
(
"Please select a Image to upload"
,
"User"
,
"Image upload failed"
);
}
// Validate file content type
String
contentType
=
file
.
getContentType
();
if
(!
ALLOWED_CONTENT_TYPES
.
contains
(
contentType
))
{
throw
new
BadRequestAlertException
(
"Invalid file type. Only JPEG, PNG, and JPG files are allowed."
,
"User"
,
"Image upload failed"
);
}
documentDto
.
setDocumentType
(
PROFILE_IMAGE
);
documentDto
.
setDocumentName
(
file
.
getOriginalFilename
());
documentDto
.
setData
(
file
.
getBytes
());
documentDto
.
setUserId
(
userId
);
// save image
documentDto
=
documentMapper
.
toDto
(
documentDao
.
save
(
documentMapper
.
toEntity
(
documentDto
)));
// response
ResponseDto
responseDto
=
new
ResponseDto
();
responseDto
.
setId
(
documentDto
.
getDocumentId
());
responseDto
.
setMessage
(
"Image uploaded successfully"
);
return
responseDto
;
}
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
throw
new
BadRequestAlertException
(
e
.
getMessage
(),
"User"
,
"error"
);
}
}
@Override
public
ResponseEntity
<
byte
[]>
getImage
(
Long
userId
)
{
log
.
info
(
"Inside user service get image method"
);
try
{
Optional
<
User
>
userOp
=
userDao
.
findById
(
userId
);
if
(
userOp
.
isEmpty
())
{
throw
new
BadRequestAlertException
(
"User not found"
,
"User"
,
"User not found"
);
}
else
{
Optional
<
Document
>
documentOp
=
documentDao
.
findByUserUserIdAndDocumentType
(
userId
,
PROFILE_IMAGE
);
if
(
documentOp
.
isEmpty
())
{
throw
new
BadRequestAlertException
(
"Image not found"
,
"User"
,
"Image not found"
);
}
else
{
DocumentDto
documentDto
=
documentMapper
.
toDto
(
documentOp
.
get
());
return
ResponseEntity
.
ok
()
.
contentType
(
MediaType
.
valueOf
(
determineImageContentType
(
documentDto
.
getDocumentName
())))
.
body
(
documentDto
.
getData
());
}
}
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
throw
new
BadRequestAlertException
(
e
.
getMessage
(),
"User"
,
"error"
);
}
}
//
@Override
//
public ResponseDto uploadImage(Long userId, MultipartFile file) {
//
log.info("Inside user service upload image method");
//
try {
//
Optional<User> userOp = userDao.findById(userId);
//
if (userOp.isEmpty()) {
//
throw new BadRequestAlertException("User not found", "User", "User not found");
//
} else {
//
Optional<Document> documentOp = documentDao.findByUserUserIdAndDocumentType(userId, PROFILE_IMAGE);
//
DocumentDto documentDto = new DocumentDto();
//
if (documentOp.isPresent()) {
//
documentDto = documentMapper.toDto(documentOp.get());
//
}
//
// save file into system
//
if (file.isEmpty()) {
//
throw new BadRequestAlertException("Please select a Image to upload", "User", "Image upload failed");
//
}
//
// Validate file content type
//
String contentType = file.getContentType();
//
if (!ALLOWED_CONTENT_TYPES.contains(contentType)) {
//
throw new BadRequestAlertException("Invalid file type. Only JPEG, PNG, and JPG files are allowed.", "User", "Image upload failed");
//
}
//
documentDto.setDocumentType(PROFILE_IMAGE);
//
documentDto.setDocumentName(file.getOriginalFilename());
//
documentDto.setData(file.getBytes());
//
documentDto.setUserId(userId);
//
//
// save image
//
documentDto = documentMapper.toDto(documentDao.save(documentMapper.toEntity(documentDto)));
//
//
// response
//
ResponseDto responseDto = new ResponseDto();
//
responseDto.setId(documentDto.getDocumentId());
//
responseDto.setMessage("Image uploaded successfully");
//
return responseDto;
//
}
//
} catch (Exception e) {
//
e.printStackTrace();
//
throw new BadRequestAlertException(e.getMessage(), "User", "error");
//
}
//
}
//
//
@Override
//
public ResponseEntity<byte[]> getImage(Long userId) {
//
log.info("Inside user service get image method");
//
try {
//
Optional<User> userOp = userDao.findById(userId);
//
if (userOp.isEmpty()) {
//
throw new BadRequestAlertException("User not found", "User", "User not found");
//
} else {
//
Optional<Document> documentOp = documentDao.findByUserUserIdAndDocumentType(userId, PROFILE_IMAGE);
//
if (documentOp.isEmpty()) {
//
throw new BadRequestAlertException("Image not found", "User", "Image not found");
//
} else {
//
DocumentDto documentDto = documentMapper.toDto(documentOp.get());
//
return ResponseEntity.ok()
//
.contentType(MediaType.valueOf(determineImageContentType(documentDto.getDocumentName())))
//
.body(documentDto.getData());
//
}
//
}
//
} catch (Exception e) {
//
e.printStackTrace();
//
throw new BadRequestAlertException(e.getMessage(), "User", "error");
//
}
//
}
}
src/main/java/com/kaluwa/enterprises/babycarebackendservice/utils/Utils.java
View file @
045a2635
...
...
@@ -4,6 +4,7 @@ import org.springframework.web.multipart.MultipartFile;
import
java.util.Arrays
;
import
java.util.List
;
import
java.util.UUID
;
public
class
Utils
{
...
...
@@ -14,7 +15,7 @@ public class Utils {
"image/jpg"
);
public
static
final
String
determineImageContentType
(
String
filename
)
{
public
static
String
determineImageContentType
(
String
filename
)
{
String
contentType
;
if
(
filename
.
endsWith
(
".jpeg"
))
{
contentType
=
"image/jpeg"
;
...
...
@@ -28,4 +29,8 @@ public class Utils {
return
contentType
;
}
public
static
String
uniqKeyGenerator
()
{
return
UUID
.
randomUUID
().
toString
().
substring
(
0
,
8
);
}
}
src/main/resources/application.yaml
View file @
045a2635
...
...
@@ -6,7 +6,7 @@ spring:
name
:
user
password
:
password
datasource
:
url
:
jdbc:mysql://1
24.43.79.107:33
306/baby_care_db?useSSL=false&serverTimezone=UTC
url
:
jdbc:mysql://1
92.168.1.8:30
306/baby_care_db?useSSL=false&serverTimezone=UTC
username
:
root
password
:
password
driver-class-name
:
com.mysql.cj.jdbc.Driver
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment