Skip to content
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

Closed
wants to merge 16 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@ Mention all related repos and projects.

Guide users through getting your code up and running on their own system. In this section you can talk about:
1. Installation process
1.2 install [Docker](https://docs.docker.com/install/)
1.3 install `docker-compose`
* with [pip](https://packaging.python.org/tutorials/installing-packages/): `pip install docker-compose`
1.3 create and run docker images for all server dependencies (mysql for now):
``` shell
docker-compose -f etc/docker/docker-compose.yml up
```

2. Software dependencies
3. Latest releases
4. API references
Expand Down
6 changes: 3 additions & 3 deletions api/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ dependencies {
implementation 'org.flywaydb:flyway-core:6.3.2'
implementation 'io.springfox:springfox-swagger2:2.9.2'
implementation 'io.springfox:springfox-swagger-ui:2.9.2'
implementation 'org.modelmapper:modelmapper:2.3.6'
implementation 'org.apache.commons:commons-lang3:3.9'

runtimeOnly 'com.h2database:h2'
runtimeOnly 'mysql:mysql-connector-java'

testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
testImplementation('org.springframework.boot:spring-boot-starter-test')
testImplementation 'org.springframework.security:spring-security-test'
}

Expand Down
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();
Copy link

@chalx chalx Mar 31, 2020

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


addCustomMappings();
addCustomTypeMaps();
}

private void addCustomMappings() {
modelMapper.createTypeMap(BaseEntity.class, BaseEntityDto.class)
Copy link

Choose a reason for hiding this comment

The 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)
Copy link

Choose a reason for hiding this comment

The 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
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

De ce nu folosesti direct @ Data?

Copy link
Author

Choose a reason for hiding this comment

The 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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Data aduce mai multe. Oare avem nevoie de tot ce aduce? eu cred ca nu

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nu iti aduce decat hashCode, equals si toString

Copy link

Choose a reason for hiding this comment

The 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

Choose a reason for hiding this comment

The 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;
}
}
Loading