-
-
Notifications
You must be signed in to change notification settings - Fork 27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added EmergencyContact entity and CRUD for it #41
Changes from all commits
aad7c2f
742b896
e2e1446
6c10112
27ff8d6
503207d
701100a
ec59160
3bbfca9
79d9a05
81e1e5e
4f4a84b
b2d1184
822fc83
8b9987b
53a5da8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.code4ro.nextdoor.core.dto; | ||
|
||
import lombok.Getter; | ||
import lombok.Setter; | ||
|
||
import java.time.LocalDateTime; | ||
import java.util.UUID; | ||
|
||
@Setter | ||
@Getter | ||
public class BaseEntityDto { | ||
private UUID id; | ||
private LocalDateTime creationDate; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.code4ro.nextdoor.core.service; | ||
|
||
import org.springframework.data.domain.Page; | ||
|
||
import java.util.List; | ||
|
||
public interface MapperService { | ||
<T> T map(Object source, Class<T> targetType); | ||
|
||
<T> List<T> mapList(List<?> sourceList, Class<? extends T> targetClass); | ||
|
||
<T> Page<T> mapPage(Page<?> sourcePage, Class<? extends T> targetClass); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package com.code4ro.nextdoor.core.service.impl; | ||
|
||
import com.code4ro.nextdoor.core.dto.BaseEntityDto; | ||
import com.code4ro.nextdoor.core.entity.BaseEntity; | ||
import com.code4ro.nextdoor.core.service.MapperService; | ||
import com.code4ro.nextdoor.emergency.contact.dto.EmergencyContactDto; | ||
import com.code4ro.nextdoor.emergency.contact.entity.EmergencyContact; | ||
import com.code4ro.nextdoor.group.dto.GroupDto; | ||
import com.code4ro.nextdoor.group.dto.GroupSecurityPolicyDto; | ||
import com.code4ro.nextdoor.group.entity.Group; | ||
import com.code4ro.nextdoor.group.entity.GroupSecurityPolicy; | ||
import org.modelmapper.AbstractConverter; | ||
import org.modelmapper.ModelMapper; | ||
import org.springframework.context.ApplicationListener; | ||
import org.springframework.context.event.ContextRefreshedEvent; | ||
import org.springframework.data.domain.Page; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.util.Collection; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
@Service | ||
public class MapperServiceImpl implements MapperService, ApplicationListener<ContextRefreshedEvent> { | ||
private final ModelMapper modelMapper; | ||
|
||
public MapperServiceImpl() { | ||
this.modelMapper = new ModelMapper(); | ||
|
||
addCustomMappings(); | ||
addCustomTypeMaps(); | ||
} | ||
|
||
private void addCustomMappings() { | ||
modelMapper.createTypeMap(BaseEntity.class, BaseEntityDto.class) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Toate astea pot fi mutate acolo unde se creeaza ModelMapper. |
||
.addMapping(BaseEntity::getId, BaseEntityDto::setId); | ||
modelMapper.createTypeMap(BaseEntityDto.class, BaseEntity.class) | ||
.addMapping(BaseEntityDto::getId, BaseEntity::setId); | ||
} | ||
|
||
private void addCustomTypeMaps() { | ||
modelMapper.createTypeMap(Group.class, GroupDto.class) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. si astea la fel |
||
.includeBase(BaseEntity.class, BaseEntityDto.class); | ||
|
||
modelMapper.createTypeMap(GroupSecurityPolicy.class, GroupSecurityPolicyDto.class); | ||
|
||
modelMapper.createTypeMap(EmergencyContact.class, EmergencyContactDto.class); | ||
modelMapper.createTypeMap(EmergencyContactDto.class, EmergencyContact.class); | ||
} | ||
|
||
@Override | ||
public <T> T map(final Object source, final Class<T> targetType) { | ||
return source != null ? modelMapper.map(source, targetType) : null; | ||
} | ||
|
||
@Override | ||
public <T> List<T> mapList(final List<?> sourceList, final Class<? extends T> targetClass) { | ||
if (sourceList == null) { | ||
return Collections.emptyList(); | ||
} | ||
|
||
return sourceList.stream() | ||
.map(listElement -> modelMapper.map(listElement, targetClass)) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
@Override | ||
public <T> Page<T> mapPage(final Page<?> sourcePage, final Class<? extends T> targetClass) { | ||
if (sourcePage == null) { | ||
return Page.empty(); | ||
} | ||
|
||
return sourcePage.map(pageElement -> modelMapper.map(pageElement, targetClass)); | ||
} | ||
|
||
@Override | ||
public void onApplicationEvent(final ContextRefreshedEvent event) { | ||
final Collection<AbstractConverter> converters = | ||
event.getApplicationContext().getBeansOfType(AbstractConverter.class).values(); | ||
converters.forEach(modelMapper::addConverter); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package com.code4ro.nextdoor.emergency.contact.controller; | ||
|
||
import com.code4ro.nextdoor.core.exception.NextDoorValidationException; | ||
import com.code4ro.nextdoor.emergency.contact.dto.EmergencyContactDto; | ||
import com.code4ro.nextdoor.emergency.contact.service.EmergencyContactService; | ||
import io.swagger.annotations.Api; | ||
import io.swagger.annotations.ApiOperation; | ||
import java.util.List; | ||
import java.util.Optional; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.DeleteMapping; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.PutMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
@Api(value = "Emergency Contact CRUD Controller") | ||
@RequestMapping("/api/emergency-contacts") | ||
public class EmergencyContactController { | ||
|
||
private final EmergencyContactService emergencyContactService; | ||
|
||
@Autowired | ||
public EmergencyContactController(EmergencyContactService emergencyContactService) { | ||
this.emergencyContactService = emergencyContactService; | ||
} | ||
|
||
@PostMapping | ||
@ApiOperation(value = "Saves an Emergency Contact") | ||
public ResponseEntity<EmergencyContactDto> save(@RequestBody EmergencyContactDto emergencyContactDto) { | ||
final EmergencyContactDto savedEmergencyContact = | ||
emergencyContactService.save(emergencyContactDto); | ||
|
||
return new ResponseEntity<>(savedEmergencyContact, HttpStatus.CREATED); | ||
} | ||
|
||
@PutMapping | ||
@ApiOperation(value = "Updates an Emergency Contact") | ||
public ResponseEntity<EmergencyContactDto> update(@RequestBody EmergencyContactDto emergencyContactDto) { | ||
final EmergencyContactDto savedEmergencyContact = | ||
emergencyContactService.update(emergencyContactDto); | ||
|
||
return ResponseEntity.ok(savedEmergencyContact); | ||
} | ||
|
||
@DeleteMapping("/{id}") | ||
@ApiOperation(value = "Deletes an Emergency Contact by id") | ||
public ResponseEntity<Void> deleteById(@PathVariable("id") String id) { | ||
emergencyContactService.deleteById(id); | ||
|
||
return new ResponseEntity<>(HttpStatus.NO_CONTENT); | ||
} | ||
|
||
@GetMapping("/{id}") | ||
@ApiOperation(value = "Gets an Emergency Contact by id") | ||
public ResponseEntity<EmergencyContactDto> getById(@PathVariable("id") String id) { | ||
Optional<EmergencyContactDto> emergencyContactDto = | ||
emergencyContactService.findByUUID(id); | ||
|
||
return emergencyContactDto.map(ResponseEntity::ok) | ||
.orElseThrow(() -> new NextDoorValidationException("id.not.found", HttpStatus.NOT_FOUND)); | ||
} | ||
|
||
@GetMapping | ||
@ApiOperation(value = "Gets all Emergency Contacts") | ||
public ResponseEntity<List<EmergencyContactDto>> getAll() { | ||
final List<EmergencyContactDto> emergencyContactDtoList = | ||
emergencyContactService.findAll(); | ||
|
||
return ResponseEntity.ok(emergencyContactDtoList); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package com.code4ro.nextdoor.emergency.contact.dto; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import lombok.Setter; | ||
|
||
@Getter | ||
@Setter | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. De ce nu folosesti direct @ Data? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Am urmarit exemplul care era in proiect, ar arata mai bine cu @DaTa intr-adevar There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nu iti aduce decat hashCode, equals si toString There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ceea ce te poate ajuta sa afisez in loguri exact obiectul, poate avem nevoie la un moment dat sa facem equals, mai ales daca vrem sa facem un diff si trebuie sa stim daca sunt egale sau nu. Deci nu vad o problema sa folosim @DaTa There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Da, 3 lucruri pe care nu le folosesti. Adica lombok e foarte bun, dar trebuie folosit doar pentru ce ai nevoie, ca sa nu poluezi codul |
||
@Builder | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
public class EmergencyContactDto { | ||
private String id; | ||
|
||
private String name; | ||
private String surname; | ||
private String email; | ||
private String address; | ||
private String telephoneNumber; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package com.code4ro.nextdoor.emergency.contact.entity; | ||
|
||
import com.code4ro.nextdoor.core.entity.BaseEntity; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import lombok.Setter; | ||
|
||
import javax.persistence.Column; | ||
import javax.persistence.Entity; | ||
|
||
@Entity | ||
@Getter | ||
@Setter | ||
@Builder | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
public class EmergencyContact extends BaseEntity { | ||
private String name; | ||
private String surname; | ||
@Column(unique = true) | ||
private String email; | ||
private String address; | ||
private String telephoneNumber; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.code4ro.nextdoor.emergency.contact.repository; | ||
|
||
import com.code4ro.nextdoor.emergency.contact.entity.EmergencyContact; | ||
import java.util.UUID; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
public interface EmergencyContactRepository extends JpaRepository<EmergencyContact, UUID> { | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.code4ro.nextdoor.emergency.contact.service; | ||
|
||
import com.code4ro.nextdoor.emergency.contact.dto.EmergencyContactDto; | ||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
public interface EmergencyContactService { | ||
|
||
EmergencyContactDto save(EmergencyContactDto emergencyContactDto); | ||
|
||
EmergencyContactDto update(EmergencyContactDto emergencyContactDto); | ||
|
||
void deleteById(String id); | ||
|
||
Optional<EmergencyContactDto> findByUUID(String id); | ||
|
||
List<EmergencyContactDto> findAll(); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package com.code4ro.nextdoor.emergency.contact.service.impl; | ||
|
||
import com.code4ro.nextdoor.core.service.MapperService; | ||
import com.code4ro.nextdoor.emergency.contact.dto.EmergencyContactDto; | ||
import com.code4ro.nextdoor.emergency.contact.entity.EmergencyContact; | ||
import com.code4ro.nextdoor.emergency.contact.repository.EmergencyContactRepository; | ||
import com.code4ro.nextdoor.emergency.contact.service.EmergencyContactService; | ||
import java.util.List; | ||
import java.util.Optional; | ||
import java.util.UUID; | ||
import java.util.stream.Collectors; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
public class EmergencyContactServiceImpl implements EmergencyContactService { | ||
|
||
private final EmergencyContactRepository emergencyContactRepository; | ||
private final MapperService mapperService; | ||
|
||
@Autowired | ||
public EmergencyContactServiceImpl(EmergencyContactRepository emergencyContactRepository, MapperService mapperService) { | ||
this.emergencyContactRepository = emergencyContactRepository; | ||
this.mapperService = mapperService; | ||
} | ||
|
||
@Override | ||
public EmergencyContactDto save(EmergencyContactDto emergencyContactDto) { | ||
final EmergencyContact emergencyContact = mapperService.map(emergencyContactDto, EmergencyContact.class); | ||
final EmergencyContact emergencyContactDB = emergencyContactRepository.save(emergencyContact); | ||
|
||
return mapperService.map(emergencyContactDB, EmergencyContactDto.class); | ||
} | ||
|
||
@Override | ||
public EmergencyContactDto update(EmergencyContactDto emergencyContactDto) { | ||
return emergencyContactRepository.findById(UUID.fromString(emergencyContactDto.getId())) | ||
.map(emergencyContact -> updateEntityWithDataFromDto(emergencyContact, emergencyContactDto)) | ||
.map(emergencyContactRepository::save) | ||
.map(emergencyContact -> mapperService.map(emergencyContact, EmergencyContactDto.class)) | ||
.orElseGet(() -> save(emergencyContactDto)); | ||
} | ||
|
||
@Override | ||
public void deleteById(String id) { | ||
emergencyContactRepository.deleteById(UUID.fromString(id)); | ||
} | ||
|
||
@Override | ||
public Optional<EmergencyContactDto> findByUUID(String id) { | ||
return emergencyContactRepository.findById(UUID.fromString(id)) | ||
.map(emergencyContact -> mapperService.map(emergencyContact, EmergencyContactDto.class)); | ||
} | ||
|
||
@Override | ||
public List<EmergencyContactDto> findAll() { | ||
return emergencyContactRepository.findAll() | ||
.stream() | ||
.map(emergencyContact -> mapperService.map(emergencyContact, EmergencyContactDto.class)) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
private EmergencyContact updateEntityWithDataFromDto(EmergencyContact emergencyContact, EmergencyContactDto emergencyContactDto) { | ||
emergencyContact.setAddress(emergencyContactDto.getAddress()); | ||
emergencyContact.setEmail(emergencyContactDto.getEmail()); | ||
emergencyContact.setSurname(emergencyContactDto.getSurname()); | ||
emergencyContact.setName(emergencyContactDto.getName()); | ||
emergencyContact.setName(emergencyContactDto.getName()); | ||
emergencyContact.setTelephoneNumber(emergencyContactDto.getTelephoneNumber()); | ||
|
||
return emergencyContact; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Model mapper poate fi creat cu @bean in configuration si sa il dam ca si parametru cu @Inject