-
-
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 #48
Open
alecstincu
wants to merge
2
commits into
code4romania:develop
Choose a base branch
from
alecstincu:emergency-contacts-new
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
...in/java/com/code4ro/nextdoor/emergency/contact/controller/EmergencyContactController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
api/src/main/java/com/code4ro/nextdoor/emergency/contact/dto/EmergencyContactDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
@Builder | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
public class EmergencyContactDto { | ||
private String id; | ||
|
||
private String firstName; | ||
private String surname; | ||
private String email; | ||
private String address; | ||
private String telephoneNumber; | ||
} |
26 changes: 26 additions & 0 deletions
26
api/src/main/java/com/code4ro/nextdoor/emergency/contact/entity/EmergencyContact.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 firstName; | ||
private String surname; | ||
@Column(unique = true) | ||
private String email; | ||
private String address; | ||
private String telephoneNumber; | ||
} |
8 changes: 8 additions & 0 deletions
8
...in/java/com/code4ro/nextdoor/emergency/contact/repository/EmergencyContactRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> { | ||
} |
18 changes: 18 additions & 0 deletions
18
...src/main/java/com/code4ro/nextdoor/emergency/contact/service/EmergencyContactService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
72 changes: 72 additions & 0 deletions
72
...java/com/code4ro/nextdoor/emergency/contact/service/impl/EmergencyContactServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
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.setFirstName(emergencyContactDto.getFirstName()); | ||
emergencyContact.setSurname(emergencyContactDto.getSurname()); | ||
emergencyContact.setEmail(emergencyContactDto.getEmail()); | ||
emergencyContact.setAddress(emergencyContactDto.getAddress()); | ||
emergencyContact.setTelephoneNumber(emergencyContactDto.getTelephoneNumber()); | ||
|
||
return emergencyContact; | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
api/src/test/java/com/code4ro/nextdoor/emergency/contact/EmergencyContactFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package com.code4ro.nextdoor.emergency.contact; | ||
|
||
import com.code4ro.nextdoor.core.RandomObjectFiller; | ||
import com.code4ro.nextdoor.emergency.contact.dto.EmergencyContactDto; | ||
import com.code4ro.nextdoor.emergency.contact.entity.EmergencyContact; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.IntStream; | ||
|
||
public class EmergencyContactFactory { | ||
|
||
public static EmergencyContact createEntity() { | ||
return RandomObjectFiller.createAndFill(EmergencyContact.class); | ||
} | ||
|
||
public static EmergencyContactDto createDto() { | ||
return RandomObjectFiller.createAndFill(EmergencyContactDto.class); | ||
} | ||
|
||
public static List<EmergencyContact> createEntityList() { | ||
return IntStream.range(1, 10) | ||
.boxed() | ||
.map(element -> RandomObjectFiller.createAndFill(EmergencyContact.class)) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
public static List<EmergencyContactDto> createDtoList() { | ||
return IntStream.range(1, 10) | ||
.boxed() | ||
.map(element -> RandomObjectFiller.createAndFill(EmergencyContactDto.class)) | ||
.collect(Collectors.toList()); | ||
} | ||
} |
104 changes: 104 additions & 0 deletions
104
api/src/test/java/com/code4ro/nextdoor/emergency/contact/EmergencyContactServiceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
package com.code4ro.nextdoor.emergency.contact; | ||
|
||
import static com.code4ro.nextdoor.emergency.contact.EmergencyContactFactory.createEntityList; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.mockito.Mockito.doNothing; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.when; | ||
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.impl.EmergencyContactServiceImpl; | ||
import java.util.List; | ||
import java.util.Optional; | ||
import java.util.UUID; | ||
import org.junit.Test; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.MockitoJUnitRunner; | ||
|
||
@RunWith(MockitoJUnitRunner.class) | ||
public class EmergencyContactServiceTest { | ||
|
||
private static final String ID = "14022837-be98-4457-af25-075d7a136a33"; | ||
|
||
@InjectMocks | ||
private EmergencyContactServiceImpl underTest; | ||
|
||
@Mock | ||
private EmergencyContactRepository emergencyContactRepository; | ||
|
||
@Mock | ||
private MapperService mapperService; | ||
|
||
@Test | ||
@DisplayName("Save emergency contact") | ||
public void testSave() { | ||
final EmergencyContactDto createDto = EmergencyContactFactory.createDto(); | ||
final EmergencyContact entity = EmergencyContactFactory.createEntity(); | ||
|
||
when(mapperService.map(createDto, EmergencyContact.class)).thenReturn(entity); | ||
when(emergencyContactRepository.save(entity)).thenReturn(entity); | ||
when(mapperService.map(entity, EmergencyContactDto.class)).thenReturn(createDto); | ||
|
||
EmergencyContactDto result = underTest.save(createDto); | ||
|
||
verify(emergencyContactRepository).save(entity); | ||
assertThat(result).isNotNull(); | ||
assertThat(result).isEqualTo(createDto); | ||
} | ||
|
||
@Test | ||
@DisplayName("Search and find an emergency contact by UUID") | ||
public void testFindByUUID() { | ||
final EmergencyContactDto dto = EmergencyContactFactory.createDto(); | ||
final EmergencyContact entity = EmergencyContactFactory.createEntity(); | ||
|
||
when(emergencyContactRepository.findById(UUID.fromString(ID))).thenReturn(Optional.of(entity)); | ||
when(mapperService.map(entity, EmergencyContactDto.class)).thenReturn(dto); | ||
|
||
Optional<EmergencyContactDto> result = underTest.findByUUID(ID); | ||
|
||
verify(emergencyContactRepository).findById(UUID.fromString(ID)); | ||
assertThat(result).isNotNull(); | ||
assertThat(result.get()).isEqualTo(dto); | ||
} | ||
|
||
@Test | ||
@DisplayName("Search an emergency contact by a non existing UUID in DB") | ||
public void testFindByNonExistingUUID() { | ||
when(emergencyContactRepository.findById(UUID.fromString(ID))).thenReturn(Optional.empty()); | ||
|
||
Optional<EmergencyContactDto> result = underTest.findByUUID(ID); | ||
|
||
verify(emergencyContactRepository).findById(UUID.fromString(ID)); | ||
assertThat(result).isEmpty(); | ||
} | ||
|
||
@Test | ||
@DisplayName("Delete emergency contact by id") | ||
public void testDeleteById() { | ||
doNothing().when(emergencyContactRepository).deleteById(UUID.fromString(ID)); | ||
|
||
underTest.deleteById(ID); | ||
|
||
verify(emergencyContactRepository).deleteById(UUID.fromString(ID)); | ||
} | ||
|
||
@Test | ||
@DisplayName("Find all emergency contacts in database") | ||
public void testFindAll() { | ||
List<EmergencyContact> entityList = createEntityList(); | ||
|
||
when(emergencyContactRepository.findAll()).thenReturn(entityList); | ||
|
||
List<EmergencyContactDto> result = underTest.findAll(); | ||
|
||
verify(emergencyContactRepository).findAll(); | ||
assertThat(result).isNotEmpty(); | ||
assertThat(result).hasSize(entityList.size()); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
if you device do add finals add them everywhere otherwise it's just noise