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
21ccb03a
Commit
21ccb03a
authored
Oct 27, 2024
by
Ishankha K.C
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'feature/chamode_dev' into 'master'
add device contact info See merge request
!8
parents
cf4fb4d5
ce6986dd
Changes
12
Hide whitespace changes
Inline
Side-by-side
Showing
12 changed files
with
365 additions
and
19 deletions
+365
-19
src/main/java/com/kaluwa/enterprises/babycarebackendservice/constants/TableNames.java
...erprises/babycarebackendservice/constants/TableNames.java
+1
-0
src/main/java/com/kaluwa/enterprises/babycarebackendservice/constants/UserRoles.java
...terprises/babycarebackendservice/constants/UserRoles.java
+3
-0
src/main/java/com/kaluwa/enterprises/babycarebackendservice/dao/ContactInfoDao.java
...nterprises/babycarebackendservice/dao/ContactInfoDao.java
+18
-0
src/main/java/com/kaluwa/enterprises/babycarebackendservice/dto/ContactInfoDto.java
...nterprises/babycarebackendservice/dto/ContactInfoDto.java
+30
-0
src/main/java/com/kaluwa/enterprises/babycarebackendservice/error/ErrorHandler.java
...nterprises/babycarebackendservice/error/ErrorHandler.java
+13
-9
src/main/java/com/kaluwa/enterprises/babycarebackendservice/global/CustomExceptionHandler.java
...babycarebackendservice/global/CustomExceptionHandler.java
+20
-7
src/main/java/com/kaluwa/enterprises/babycarebackendservice/global/ErrorResponse.java
...erprises/babycarebackendservice/global/ErrorResponse.java
+5
-3
src/main/java/com/kaluwa/enterprises/babycarebackendservice/mappers/ContactInfoMapper.java
...ses/babycarebackendservice/mappers/ContactInfoMapper.java
+26
-0
src/main/java/com/kaluwa/enterprises/babycarebackendservice/model/ContactInfo.java
...enterprises/babycarebackendservice/model/ContactInfo.java
+27
-0
src/main/java/com/kaluwa/enterprises/babycarebackendservice/rest/ContactInfoController.java
...es/babycarebackendservice/rest/ContactInfoController.java
+53
-0
src/main/java/com/kaluwa/enterprises/babycarebackendservice/service/ContactInfoService.java
...es/babycarebackendservice/service/ContactInfoService.java
+16
-0
src/main/java/com/kaluwa/enterprises/babycarebackendservice/service/impl/ContactInfoServiceImpl.java
...rebackendservice/service/impl/ContactInfoServiceImpl.java
+153
-0
No files found.
src/main/java/com/kaluwa/enterprises/babycarebackendservice/constants/TableNames.java
View file @
21ccb03a
...
@@ -6,5 +6,6 @@ public class TableNames {
...
@@ -6,5 +6,6 @@ public class TableNames {
public
final
static
String
BABY_TABLE
=
"babies"
;
public
final
static
String
BABY_TABLE
=
"babies"
;
public
final
static
String
DOCUMENT_TABLE
=
"documents"
;
public
final
static
String
DOCUMENT_TABLE
=
"documents"
;
public
final
static
String
ACTIVITY_LOG_TABLE
=
"activity_logs"
;
public
final
static
String
ACTIVITY_LOG_TABLE
=
"activity_logs"
;
public
final
static
String
CONTACT_INFO_TABLE
=
"contact_info"
;
}
}
src/main/java/com/kaluwa/enterprises/babycarebackendservice/constants/UserRoles.java
View file @
21ccb03a
...
@@ -3,4 +3,7 @@ package com.kaluwa.enterprises.babycarebackendservice.constants;
...
@@ -3,4 +3,7 @@ package com.kaluwa.enterprises.babycarebackendservice.constants;
public
class
UserRoles
{
public
class
UserRoles
{
public
static
final
String
ADMIN
=
"ADMIN"
;
public
static
final
String
ADMIN
=
"ADMIN"
;
public
static
final
String
USER
=
"USER"
;
public
static
final
String
USER
=
"USER"
;
public
static
final
String
CRADLE
=
"CRADLE"
;
public
static
final
String
NANNY
=
"NANNY"
;
}
}
src/main/java/com/kaluwa/enterprises/babycarebackendservice/dao/ContactInfoDao.java
0 → 100644
View file @
21ccb03a
package
com.kaluwa.enterprises.babycarebackendservice.dao
;
import
com.kaluwa.enterprises.babycarebackendservice.model.ContactInfo
;
import
jakarta.validation.constraints.NotEmpty
;
import
org.springframework.data.jpa.repository.JpaRepository
;
import
java.util.List
;
import
java.util.Optional
;
public
interface
ContactInfoDao
extends
JpaRepository
<
ContactInfo
,
Long
>
{
boolean
existsByUserUserIdAndType
(
Long
userId
,
String
type
);
Optional
<
ContactInfo
>
findByUserUserIdAndType
(
Long
userId
,
String
type
);
boolean
existsByUserUserIdAndTypeAndContactInfoIdNot
(
Long
userId
,
String
type
,
Long
contactInfoId
);
void
deleteByUserUserIdAndType
(
Long
userId
,
String
type
);
}
\ No newline at end of file
src/main/java/com/kaluwa/enterprises/babycarebackendservice/dto/ContactInfoDto.java
0 → 100644
View file @
21ccb03a
package
com.kaluwa.enterprises.babycarebackendservice.dto
;
import
com.kaluwa.enterprises.babycarebackendservice.model.ContactInfo
;
import
jakarta.validation.constraints.NotEmpty
;
import
jakarta.validation.constraints.NotNull
;
import
jakarta.validation.constraints.Size
;
import
lombok.*
;
import
java.io.Serializable
;
/**
* DTO for {@link ContactInfo}
*/
@AllArgsConstructor
@NoArgsConstructor
@Data
public
class
ContactInfoDto
implements
Serializable
{
private
Long
contactInfoId
;
@NotEmpty
(
message
=
"Contact name is required"
)
private
String
contactName
;
@NotEmpty
(
message
=
"Contact number is required"
)
@Size
(
min
=
10
,
max
=
10
,
message
=
"Contact number should be 10 digits"
)
private
String
contactNo
;
@NotEmpty
(
message
=
"Contact type is required"
)
private
String
type
;
private
UserDto
user
;
@NotNull
(
message
=
"User id is required"
)
private
Long
userId
;
}
\ No newline at end of file
src/main/java/com/kaluwa/enterprises/babycarebackendservice/error/ErrorHandler.java
View file @
21ccb03a
package
com.kaluwa.enterprises.babycarebackendservice.error
;
package
com.kaluwa.enterprises.babycarebackendservice.error
;
import
com.fasterxml.jackson.databind.ObjectMapper
;
import
com.fasterxml.jackson.databind.ObjectMapper
;
import
com.kaluwa.enterprises.babycarebackendservice.global.ErrorResponse
;
import
org.springframework.http.HttpStatus
;
import
org.springframework.http.HttpStatus
;
import
org.springframework.http.ResponseEntity
;
import
org.springframework.http.ResponseEntity
;
import
org.springframework.web.bind.annotation.ControllerAdvice
;
import
org.springframework.web.bind.annotation.ControllerAdvice
;
...
@@ -15,19 +16,22 @@ import java.util.Map;
...
@@ -15,19 +16,22 @@ import java.util.Map;
public
class
ErrorHandler
extends
ResponseEntityExceptionHandler
{
public
class
ErrorHandler
extends
ResponseEntityExceptionHandler
{
@ExceptionHandler
(
BadRequestAlertException
.
class
)
@ExceptionHandler
(
BadRequestAlertException
.
class
)
public
ResponseEntity
<
Object
>
handleBadRequestAlertException
(
BadRequestAlertException
ex
,
WebRequest
request
)
{
public
ResponseEntity
<
ErrorResponse
>
handleBadRequestAlertException
(
BadRequestAlertException
ex
,
WebRequest
request
)
{
Map
<
String
,
Object
>
body
=
new
LinkedHashMap
<>();
Map
<
String
,
Object
>
body
=
new
LinkedHashMap
<>();
body
.
put
(
"timestamp"
,
LocalDateTime
.
now
().
toString
());
ErrorResponse
errorResponse
=
new
ErrorResponse
();
body
.
put
(
"message"
,
ex
.
getMessage
());
errorResponse
.
setTimestamp
(
LocalDateTime
.
now
().
toString
());
body
.
put
(
"error"
,
ex
.
getErrorKey
());
errorResponse
.
setMessage
(
ex
.
getMessage
());
return
new
ResponseEntity
<>(
body
,
HttpStatus
.
BAD_REQUEST
);
errorResponse
.
setError
(
ex
.
getErrorKey
());
return
new
ResponseEntity
<>(
errorResponse
,
HttpStatus
.
BAD_REQUEST
);
}
}
@ExceptionHandler
(
AuthorizationAlertException
.
class
)
@ExceptionHandler
(
AuthorizationAlertException
.
class
)
public
ResponseEntity
<
Object
>
handleAuthorizationAlertException
(
AuthorizationAlertException
ae
,
WebRequest
request
)
{
public
ResponseEntity
<
ErrorResponse
>
handleAuthorizationAlertException
(
AuthorizationAlertException
ae
,
WebRequest
request
)
{
Map
<
String
,
Object
>
body
=
new
LinkedHashMap
<>();
Map
<
String
,
Object
>
body
=
new
LinkedHashMap
<>();
body
.
put
(
"timestamp"
,
LocalDateTime
.
now
().
toString
());
ErrorResponse
errorResponse
=
new
ErrorResponse
();
body
.
put
(
"error"
,
ae
.
getMessage
());
errorResponse
.
setTimestamp
(
LocalDateTime
.
now
().
toString
());
return
new
ResponseEntity
<>(
body
,
HttpStatus
.
UNAUTHORIZED
);
errorResponse
.
setMessage
(
ae
.
getMessage
());
errorResponse
.
setError
(
ae
.
getErrorKey
());
return
new
ResponseEntity
<>(
errorResponse
,
HttpStatus
.
UNAUTHORIZED
);
}
}
}
}
src/main/java/com/kaluwa/enterprises/babycarebackendservice/global/CustomExceptionHandler.java
View file @
21ccb03a
...
@@ -9,6 +9,12 @@ import org.springframework.web.bind.MethodArgumentNotValidException;
...
@@ -9,6 +9,12 @@ import org.springframework.web.bind.MethodArgumentNotValidException;
import
org.springframework.web.bind.annotation.ExceptionHandler
;
import
org.springframework.web.bind.annotation.ExceptionHandler
;
import
org.springframework.web.bind.annotation.RestControllerAdvice
;
import
org.springframework.web.bind.annotation.RestControllerAdvice
;
import
java.time.LocalDateTime
;
import
java.util.LinkedHashMap
;
import
java.util.List
;
import
java.util.Map
;
import
java.util.stream.Collectors
;
@RestControllerAdvice
@RestControllerAdvice
@Slf4j
@Slf4j
public
class
CustomExceptionHandler
{
public
class
CustomExceptionHandler
{
...
@@ -16,15 +22,22 @@ public class CustomExceptionHandler {
...
@@ -16,15 +22,22 @@ public class CustomExceptionHandler {
@ExceptionHandler
(
MethodArgumentNotValidException
.
class
)
@ExceptionHandler
(
MethodArgumentNotValidException
.
class
)
public
ResponseEntity
<
ErrorResponse
>
handleValidationExceptions
(
MethodArgumentNotValidException
ex
)
{
public
ResponseEntity
<
ErrorResponse
>
handleValidationExceptions
(
MethodArgumentNotValidException
ex
)
{
BindingResult
result
=
ex
.
getBindingResult
();
BindingResult
result
=
ex
.
getBindingResult
();
FieldError
error
=
result
.
getFieldError
();
List
<
FieldError
>
fieldErrors
=
result
.
getFieldErrors
();
String
message
=
error
.
getField
()+
" : "
+
error
.
getDefaultMessage
();
log
.
debug
(
"Validation error: {}"
,
message
);
// Collect all field errors into a single message
String
message
=
fieldErrors
.
stream
()
.
map
(
fieldError
->
fieldError
.
getField
()
+
" : "
+
fieldError
.
getDefaultMessage
())
.
collect
(
Collectors
.
joining
(
", "
));
log
.
debug
(
"Validation errors: {}"
,
message
);
// Prepare the error response
ErrorResponse
errorResponse
=
new
ErrorResponse
();
ErrorResponse
errorResponse
=
new
ErrorResponse
();
errorResponse
.
setHttpCode
(
400
);
errorResponse
.
setTimestamp
(
LocalDateTime
.
now
().
toString
());
errorResponse
.
setErrorCode
(
"VALIDATION FAILED!"
);
errorResponse
.
setMessage
(
"VALIDATION FAILED!"
);
errorResponse
.
setDescription
(
message
);
errorResponse
.
setError
(
message
);
return
new
ResponseEntity
<>(
errorResponse
,
HttpStatus
.
BAD_REQUEST
);
return
new
ResponseEntity
<>(
errorResponse
,
HttpStatus
.
BAD_REQUEST
);
}
}
}
}
src/main/java/com/kaluwa/enterprises/babycarebackendservice/global/ErrorResponse.java
View file @
21ccb03a
...
@@ -4,11 +4,13 @@ import lombok.AllArgsConstructor;
...
@@ -4,11 +4,13 @@ import lombok.AllArgsConstructor;
import
lombok.Data
;
import
lombok.Data
;
import
lombok.NoArgsConstructor
;
import
lombok.NoArgsConstructor
;
import
java.time.LocalDateTime
;
@Data
@Data
@AllArgsConstructor
@AllArgsConstructor
@NoArgsConstructor
@NoArgsConstructor
public
class
ErrorResponse
{
public
class
ErrorResponse
{
private
int
httpCode
;
private
String
timestamp
;
private
String
errorCod
e
;
private
String
messag
e
;
private
String
description
;
private
String
error
;
}
}
src/main/java/com/kaluwa/enterprises/babycarebackendservice/mappers/ContactInfoMapper.java
0 → 100644
View file @
21ccb03a
package
com.kaluwa.enterprises.babycarebackendservice.mappers
;
import
com.kaluwa.enterprises.babycarebackendservice.dto.ContactInfoDto
;
import
com.kaluwa.enterprises.babycarebackendservice.model.ContactInfo
;
import
org.mapstruct.*
;
import
java.util.List
;
@Mapper
(
componentModel
=
"spring"
)
public
interface
ContactInfoMapper
{
@Mappings
({
@Mapping
(
target
=
"user.userId"
,
source
=
"userId"
)
})
ContactInfo
toEntity
(
ContactInfoDto
contactInfoDto
);
@Mappings
({
@Mapping
(
source
=
"user.userId"
,
target
=
"userId"
)
})
ContactInfoDto
toDto
(
ContactInfo
contactInfo
);
@Mappings
({
@Mapping
(
source
=
"user.userId"
,
target
=
"userId"
)
})
List
<
ContactInfoDto
>
listToDto
(
List
<
ContactInfo
>
contactInfos
);
}
\ No newline at end of file
src/main/java/com/kaluwa/enterprises/babycarebackendservice/model/ContactInfo.java
0 → 100644
View file @
21ccb03a
package
com.kaluwa.enterprises.babycarebackendservice.model
;
import
jakarta.persistence.*
;
import
lombok.AllArgsConstructor
;
import
lombok.Data
;
import
lombok.NoArgsConstructor
;
import
static
com
.
kaluwa
.
enterprises
.
babycarebackendservice
.
constants
.
TableNames
.
CONTACT_INFO_TABLE
;
@AllArgsConstructor
@NoArgsConstructor
@Data
@Entity
@Table
(
name
=
CONTACT_INFO_TABLE
)
public
class
ContactInfo
{
@Id
@GeneratedValue
(
strategy
=
GenerationType
.
AUTO
)
private
Long
contactInfoId
;
private
String
contactName
;
private
String
contactNo
;
private
String
type
;
@ManyToOne
(
fetch
=
FetchType
.
LAZY
)
@JoinColumn
(
name
=
"userId"
,
referencedColumnName
=
"userId"
,
nullable
=
false
)
private
User
user
;
}
src/main/java/com/kaluwa/enterprises/babycarebackendservice/rest/ContactInfoController.java
0 → 100644
View file @
21ccb03a
package
com.kaluwa.enterprises.babycarebackendservice.rest
;
import
com.kaluwa.enterprises.babycarebackendservice.dto.ContactInfoDto
;
import
com.kaluwa.enterprises.babycarebackendservice.dto.ResponseDto
;
import
com.kaluwa.enterprises.babycarebackendservice.service.ContactInfoService
;
import
jakarta.validation.Valid
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.http.ResponseEntity
;
import
org.springframework.validation.annotation.Validated
;
import
org.springframework.web.bind.annotation.*
;
@RestController
@RequestMapping
(
"/contact-info"
)
@Slf4j
public
class
ContactInfoController
{
private
final
ContactInfoService
contactInfoService
;
public
ContactInfoController
(
ContactInfoService
contactInfoService
)
{
this
.
contactInfoService
=
contactInfoService
;
}
@PostMapping
public
ResponseEntity
<
ContactInfoDto
>
createContactInfo
(
@RequestBody
ContactInfoDto
contactInfo
)
{
log
.
info
(
"Inside ContactInfoController.createContactInfo"
);
return
contactInfoService
.
createContactInfo
(
contactInfo
);
}
@GetMapping
(
"/user-id/{userId}/{type}"
)
public
ResponseEntity
<
ContactInfoDto
>
getContactInfoByUserId
(
@PathVariable
Long
userId
,
@PathVariable
String
type
)
{
log
.
info
(
"Inside ContactInfoController.getContactInfoByUserId"
);
return
contactInfoService
.
getContactInfoByUserId
(
userId
,
type
);
}
@PutMapping
(
"/{contactInfoId}"
)
public
ResponseEntity
<
ContactInfoDto
>
updateContactInfo
(
@PathVariable
Long
contactInfoId
,
@RequestBody
ContactInfoDto
contactInfo
)
{
log
.
info
(
"Inside ContactInfoController.updateContactInfo"
);
return
contactInfoService
.
updateContactInfo
(
contactInfoId
,
contactInfo
);
}
@DeleteMapping
(
"/user-id/{userId}/{type}"
)
public
ResponseEntity
<
ResponseDto
>
deleteContactInfoByUserId
(
@PathVariable
Long
userId
,
@PathVariable
String
type
)
{
log
.
info
(
"Inside ContactInfoController.deleteContactInfoByUserId"
);
return
contactInfoService
.
deleteContactInfoByUserId
(
userId
,
type
);
}
}
src/main/java/com/kaluwa/enterprises/babycarebackendservice/service/ContactInfoService.java
0 → 100644
View file @
21ccb03a
package
com.kaluwa.enterprises.babycarebackendservice.service
;
import
com.kaluwa.enterprises.babycarebackendservice.dto.ContactInfoDto
;
import
com.kaluwa.enterprises.babycarebackendservice.dto.ResponseDto
;
import
jakarta.validation.Valid
;
import
org.springframework.http.ResponseEntity
;
public
interface
ContactInfoService
{
ResponseEntity
<
ContactInfoDto
>
createContactInfo
(
@Valid
ContactInfoDto
contactInfo
);
ResponseEntity
<
ContactInfoDto
>
getContactInfoByUserId
(
Long
userId
,
String
type
);
ResponseEntity
<
ContactInfoDto
>
updateContactInfo
(
Long
contactInfoId
,
@Valid
ContactInfoDto
contactInfo
);
ResponseEntity
<
ResponseDto
>
deleteContactInfoByUserId
(
Long
userId
,
String
type
);
}
src/main/java/com/kaluwa/enterprises/babycarebackendservice/service/impl/ContactInfoServiceImpl.java
0 → 100644
View file @
21ccb03a
package
com.kaluwa.enterprises.babycarebackendservice.service.impl
;
import
com.kaluwa.enterprises.babycarebackendservice.dao.ContactInfoDao
;
import
com.kaluwa.enterprises.babycarebackendservice.dto.ContactInfoDto
;
import
com.kaluwa.enterprises.babycarebackendservice.dto.ResponseDto
;
import
com.kaluwa.enterprises.babycarebackendservice.error.BadRequestAlertException
;
import
com.kaluwa.enterprises.babycarebackendservice.mappers.ContactInfoMapper
;
import
com.kaluwa.enterprises.babycarebackendservice.model.ContactInfo
;
import
com.kaluwa.enterprises.babycarebackendservice.service.ContactInfoService
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.http.ResponseEntity
;
import
org.springframework.stereotype.Service
;
import
java.util.Optional
;
import
static
com
.
kaluwa
.
enterprises
.
babycarebackendservice
.
constants
.
UserRoles
.
CRADLE
;
import
static
com
.
kaluwa
.
enterprises
.
babycarebackendservice
.
constants
.
UserRoles
.
NANNY
;
@Service
@Slf4j
public
class
ContactInfoServiceImpl
implements
ContactInfoService
{
private
final
ContactInfoDao
contactInfoDao
;
private
final
ContactInfoMapper
contactInfoMapper
;
public
ContactInfoServiceImpl
(
ContactInfoDao
contactInfoDao
,
ContactInfoMapper
contactInfoMapper
)
{
this
.
contactInfoDao
=
contactInfoDao
;
this
.
contactInfoMapper
=
contactInfoMapper
;
}
@Override
public
ResponseEntity
<
ContactInfoDto
>
createContactInfo
(
ContactInfoDto
contactInfo
)
{
log
.
info
(
"Inside ContactInfoServiceImpl.createContactInfo"
);
try
{
// validate
if
(
contactInfo
.
getType
().
isEmpty
())
{
throw
new
BadRequestAlertException
(
"Contact type is required"
,
"ContactInfo"
,
"ContactTypeRequired"
);
}
if
(
contactInfo
.
getType
().
equalsIgnoreCase
(
CRADLE
))
{
contactInfo
.
setType
(
CRADLE
);
}
else
if
(
contactInfo
.
getType
().
equalsIgnoreCase
(
NANNY
))
{
contactInfo
.
setType
(
NANNY
);
}
else
{
throw
new
BadRequestAlertException
(
"Invalid contact type"
,
"ContactInfo"
,
"InvalidContactType"
);
}
if
(
contactInfo
.
getContactName
().
isEmpty
())
{
throw
new
BadRequestAlertException
(
"Contact name is required"
,
"ContactInfo"
,
"ContactNameRequired"
);
}
if
(
contactInfo
.
getContactNo
().
isEmpty
())
{
throw
new
BadRequestAlertException
(
"Contact number is required"
,
"ContactInfo"
,
"ContactNoRequired"
);
}
if
(
contactInfo
.
getContactNo
().
length
()
!=
10
)
{
throw
new
BadRequestAlertException
(
"Contact number should be 10 digits"
,
"ContactInfo"
,
"InvalidContactNo"
);
}
if
(
contactInfo
.
getUserId
()
==
null
)
{
throw
new
BadRequestAlertException
(
"User id is required"
,
"ContactInfo"
,
"UserIdRequired"
);
}
if
(
contactInfoDao
.
existsByUserUserIdAndType
(
contactInfo
.
getUserId
(),
contactInfo
.
getType
()))
{
throw
new
BadRequestAlertException
(
"Contact info already exists for you"
,
"ContactInfo"
,
"ContactInfoExists"
);
}
// create
return
ResponseEntity
.
ok
(
contactInfoMapper
.
toDto
(
contactInfoDao
.
save
(
contactInfoMapper
.
toEntity
(
contactInfo
))));
}
catch
(
Exception
e
)
{
log
.
error
(
"Error occurred while creating contact info"
,
e
);
e
.
printStackTrace
();
throw
new
BadRequestAlertException
(
e
.
getMessage
(),
"Error"
,
"Error"
);
}
}
@Override
public
ResponseEntity
<
ContactInfoDto
>
getContactInfoByUserId
(
Long
userId
,
String
type
)
{
log
.
info
(
"Inside ContactInfoServiceImpl.getContactInfoByUserId"
);
try
{
// validate
if
(!
contactInfoDao
.
existsByUserUserIdAndType
(
userId
,
type
))
{
throw
new
BadRequestAlertException
(
"Contact info does not exist for you, Please add first"
,
"ContactInfo"
,
"ContactInfoDoesNotExist"
);
}
return
ResponseEntity
.
ok
(
contactInfoMapper
.
toDto
(
contactInfoDao
.
findByUserUserIdAndType
(
userId
,
type
).
get
()));
}
catch
(
Exception
e
)
{
log
.
error
(
"Error occurred while getting contact info"
,
e
);
e
.
printStackTrace
();
throw
new
BadRequestAlertException
(
e
.
getMessage
(),
"Error"
,
"Error"
);
}
}
@Override
public
ResponseEntity
<
ContactInfoDto
>
updateContactInfo
(
Long
contactInfoId
,
ContactInfoDto
contactInfo
)
{
log
.
info
(
"Inside ContactInfoServiceImpl.updateContactInfo"
);
try
{
// validate
if
(
contactInfo
.
getContactInfoId
()
==
null
)
{
throw
new
BadRequestAlertException
(
"Contact info id is required"
,
"ContactInfo"
,
"ContactInfoIdRequired"
);
}
if
(
contactInfo
.
getType
().
isEmpty
())
{
throw
new
BadRequestAlertException
(
"Contact type is required"
,
"ContactInfo"
,
"ContactTypeRequired"
);
}
if
(!
contactInfoId
.
equals
(
contactInfo
.
getContactInfoId
()))
{
throw
new
BadRequestAlertException
(
"Invalid contact info id"
,
"ContactInfo"
,
"InvalidContactInfoId"
);
}
if
(
contactInfo
.
getContactName
().
isEmpty
())
{
throw
new
BadRequestAlertException
(
"Contact name is required"
,
"ContactInfo"
,
"ContactNameRequired"
);
}
if
(
contactInfo
.
getContactNo
().
isEmpty
())
{
throw
new
BadRequestAlertException
(
"Contact number is required"
,
"ContactInfo"
,
"ContactNoRequired"
);
}
if
(
contactInfo
.
getContactNo
().
length
()
!=
10
)
{
throw
new
BadRequestAlertException
(
"Contact number should be 10 digits"
,
"ContactInfo"
,
"InvalidContactNo"
);
}
if
(
contactInfo
.
getUserId
()
==
null
)
{
throw
new
BadRequestAlertException
(
"User id is required"
,
"ContactInfo"
,
"UserIdRequired"
);
}
Optional
<
ContactInfo
>
contactInfoDto
=
contactInfoDao
.
findById
(
contactInfoId
);
if
(!
contactInfoDto
.
isPresent
())
{
throw
new
BadRequestAlertException
(
"Contact info does not exist"
,
"ContactInfo"
,
"ContactInfoDoesNotExist"
);
}
if
(
contactInfo
.
getType
().
equalsIgnoreCase
(
CRADLE
))
{
contactInfo
.
setType
(
CRADLE
);
}
else
if
(
contactInfo
.
getType
().
equalsIgnoreCase
(
NANNY
))
{
contactInfo
.
setType
(
NANNY
);
}
else
{
throw
new
BadRequestAlertException
(
"Invalid contact type"
,
"ContactInfo"
,
"InvalidContactType"
);
}
if
(
contactInfoDao
.
existsByUserUserIdAndTypeAndContactInfoIdNot
(
contactInfo
.
getUserId
(),
contactInfo
.
getType
(),
contactInfoId
))
{
throw
new
BadRequestAlertException
(
"Contact info already exists for you"
,
"ContactInfo"
,
"ContactInfoExists"
);
}
// update
return
ResponseEntity
.
ok
(
contactInfoMapper
.
toDto
(
contactInfoDao
.
save
(
contactInfoMapper
.
toEntity
(
contactInfo
))));
}
catch
(
Exception
e
)
{
log
.
error
(
"Error occurred while updating contact info"
,
e
);
e
.
printStackTrace
();
throw
new
BadRequestAlertException
(
e
.
getMessage
(),
"Error"
,
"Error"
);
}
}
@Override
public
ResponseEntity
<
ResponseDto
>
deleteContactInfoByUserId
(
Long
userId
,
String
type
)
{
log
.
info
(
"Inside ContactInfoServiceImpl.deleteContactInfoByUserId"
);
try
{
// validate
if
(!
contactInfoDao
.
existsByUserUserIdAndType
(
userId
,
type
))
{
throw
new
BadRequestAlertException
(
"Contact info does not exist for you"
,
"ContactInfo"
,
"ContactInfoDoesNotExist"
);
}
// delete
contactInfoDao
.
deleteByUserUserIdAndType
(
userId
,
type
);
return
ResponseEntity
.
ok
(
new
ResponseDto
(
Long
.
parseLong
(
"200"
),
"Contact info deleted successfully"
));
}
catch
(
Exception
e
)
{
log
.
error
(
"Error occurred while deleting contact info"
,
e
);
e
.
printStackTrace
();
throw
new
BadRequestAlertException
(
e
.
getMessage
(),
"Error"
,
"Error"
);
}
}
}
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