diff --git a/pom.xml b/pom.xml
index cf21f4f..7ff1b1d 100755
--- a/pom.xml
+++ b/pom.xml
@@ -109,7 +109,15 @@
org.springframework.boot
spring-boot-maven-plugin
-
+
+ org.apache.maven.plugins
+ maven-compiler-plugin
+
+ 9
+ 9
+
+
+
diff --git a/src/main/java/br/com/clickbus/challenge/controller/PlaceController.java b/src/main/java/br/com/clickbus/challenge/controller/PlaceController.java
index bbd5d5c..e03b462 100644
--- a/src/main/java/br/com/clickbus/challenge/controller/PlaceController.java
+++ b/src/main/java/br/com/clickbus/challenge/controller/PlaceController.java
@@ -9,21 +9,19 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
-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;
+import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
+import java.util.List;
+import java.util.Optional;
+import java.util.stream.Collectors;
@Api("places")
@RestController
-@RequestMapping("places")
+@RequestMapping("/places")
public class PlaceController {
+ @Autowired
private PlaceService service;
@PostMapping
@@ -31,7 +29,7 @@ public ResponseEntity create(@RequestBody @Valid PlaceDTO dto) {
return new ResponseEntity(service.save(dto.buildPlace()).convertToDTO(), HttpStatus.CREATED);
}
- @GetMapping("{id}")
+ @GetMapping("/{id}")
public ResponseEntity findById(@PathVariable Long id) {
return service.findById(id)
.map(place -> ResponseEntity.ok(place.convertToDTO()))
@@ -49,4 +47,15 @@ public ResponseEntity alter(@PathVariable Long id, @RequestBody @Valid PlaceDTO
Place place = service.findById(id).orElseThrow(null);
return new ResponseEntity(service.alter(place, placeDTO).convertToDTO(), HttpStatus.OK);
}
+
+ @GetMapping("/")
+ public ResponseEntity findByName(@RequestParam String name){
+
+ if(service.findByName(name).isEmpty()){
+ throw new PlaceNotFoundException(HttpStatus.NOT_FOUND);
+ }
+
+ return ResponseEntity.ok(service.findByName(name).stream().map(Place::convertToDTO));
+
+ }
}
diff --git a/src/main/java/br/com/clickbus/challenge/dto/PlaceDTO.java b/src/main/java/br/com/clickbus/challenge/dto/PlaceDTO.java
index 26e878d..cf79132 100644
--- a/src/main/java/br/com/clickbus/challenge/dto/PlaceDTO.java
+++ b/src/main/java/br/com/clickbus/challenge/dto/PlaceDTO.java
@@ -35,6 +35,7 @@ public static PlaceDTO of(String name, String slug, String city, String state) {
return new PlaceDTO(name, slug, city, state);
}
+
public static Iterable convertToList(List places) {
return places.stream().map(Place::convertToDTO).collect(Collectors.toList());
}
@@ -42,4 +43,36 @@ public static Iterable convertToList(List places) {
public Place buildPlace() {
return Place.of(this.name, this.slug, this.city, this.state);
}
+
+ public @NotNull String getName() {
+ return name;
+ }
+
+ public void setName(@NotNull String name) {
+ this.name = name;
+ }
+
+ public @NotNull String getSlug() {
+ return slug;
+ }
+
+ public void setSlug(@NotNull String slug) {
+ this.slug = slug;
+ }
+
+ public @NotNull String getCity() {
+ return city;
+ }
+
+ public void setCity(@NotNull String city) {
+ this.city = city;
+ }
+
+ public @NotNull String getState() {
+ return state;
+ }
+
+ public void setState(@NotNull String state) {
+ this.state = state;
+ }
}
diff --git a/src/main/java/br/com/clickbus/challenge/entity/Place.java b/src/main/java/br/com/clickbus/challenge/entity/Place.java
index ccfc550..daee781 100644
--- a/src/main/java/br/com/clickbus/challenge/entity/Place.java
+++ b/src/main/java/br/com/clickbus/challenge/entity/Place.java
@@ -39,6 +39,10 @@ public class Place {
private LocalDateTime updatedAt;
+ public Place(){
+
+ }
+
public Place(String name, String slug, String city, String state) {
this.name = name;
this.slug = slug;
@@ -51,7 +55,63 @@ public static Place of(String name, String slug, String city, String state) {
return new Place(name, slug, city, state);
}
+ public @NotNull String getName() {
+ return name;
+ }
+
public PlaceDTO convertToDTO() {
return PlaceDTO.of(this.name, this.slug, this.city, this.state);
}
+
+ public Long getId() {
+ return id;
+ }
+
+ public void setId(Long id) {
+ this.id = id;
+ }
+
+ public void setName(@NotNull String name) {
+ this.name = name;
+ }
+
+ public @NotNull String getSlug() {
+ return slug;
+ }
+
+ public void setSlug(@NotNull String slug) {
+ this.slug = slug;
+ }
+
+ public @NotNull String getCity() {
+ return city;
+ }
+
+ public void setCity(@NotNull String city) {
+ this.city = city;
+ }
+
+ public @NotNull String getState() {
+ return state;
+ }
+
+ public void setState(@NotNull String state) {
+ this.state = state;
+ }
+
+ public LocalDateTime getCreatedAt() {
+ return createdAt;
+ }
+
+ public void setCreatedAt(LocalDateTime createdAt) {
+ this.createdAt = createdAt;
+ }
+
+ public LocalDateTime getUpdatedAt() {
+ return updatedAt;
+ }
+
+ public void setUpdatedAt(LocalDateTime updatedAt) {
+ this.updatedAt = updatedAt;
+ }
}
diff --git a/src/main/java/br/com/clickbus/challenge/service/PlaceService.java b/src/main/java/br/com/clickbus/challenge/service/PlaceService.java
index 799d1a8..2c141de 100644
--- a/src/main/java/br/com/clickbus/challenge/service/PlaceService.java
+++ b/src/main/java/br/com/clickbus/challenge/service/PlaceService.java
@@ -5,36 +5,46 @@
import br.com.clickbus.challenge.entity.Place;
import br.com.clickbus.challenge.repository.PlaceRepository;
import lombok.AllArgsConstructor;
+import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.validation.constraints.NotNull;
+import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;
-import org.apache.commons.lang3.NotImplementedException;
+
@Service
@AllArgsConstructor
public class PlaceService {
+ @Autowired
private PlaceRepository repository;
public List findAll() {
- throw new NotImplementedException("Metodo nao implementado");
+ return repository.findAll();
}
public Optional findById(@NotNull Long id) {
- throw new NotImplementedException("Metodo nao implementado");
+ return repository.findById(id);
}
public Place save(@NotNull Place place) {
- throw new NotImplementedException("Metodo nao implementado");
+ return repository.save(place);
}
public List findByName(@NotNull String name) {
- throw new NotImplementedException("Metodo nao implementado");
+ return repository.findByName(name);
}
public Place alter(@NotNull Place place,@NotNull PlaceDTO placeDTO) {
- throw new NotImplementedException("Metodo nao implementado");
+
+ place.setName(placeDTO.getName());
+ place.setSlug(placeDTO.getSlug());
+ place.setCity(place.getCity());
+ place.setState(placeDTO.getState());
+ place.setUpdatedAt(LocalDateTime.now());
+
+ return repository.save(place);
}
}
diff --git a/src/test/java/br/com/clickbus/challenge/service/PlaceServiceTest.java b/src/test/java/br/com/clickbus/challenge/service/PlaceServiceTest.java
index cf0f0a9..1ce83d4 100644
--- a/src/test/java/br/com/clickbus/challenge/service/PlaceServiceTest.java
+++ b/src/test/java/br/com/clickbus/challenge/service/PlaceServiceTest.java
@@ -1,6 +1,5 @@
package br.com.clickbus.challenge.service;
-
import br.com.clickbus.challenge.dto.PlaceDTO;
import br.com.clickbus.challenge.entity.Place;
import br.com.clickbus.challenge.repository.PlaceRepository;
@@ -11,15 +10,14 @@
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
-import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
+
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
-import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.anyLong;
import static org.mockito.Mockito.atLeastOnce;
@@ -112,4 +110,23 @@ void whenAlterPlaceOk() {
assertEquals(place.getCreatedAt(), edited.getCreatedAt());
assertNotNull(edited.getUpdatedAt());
}
+
+ @Test
+ void whenFindAllOk() {
+ when(repository.findAll()).thenReturn(Collections.singletonList(place));
+
+ List actual = service.findAll();
+
+ assertNotNull(actual);
+ assertEquals(1, actual.size());
+ verify(repository, atLeastOnce()).findAll();
+ }
+
+ @Test
+ void whenFindAllNotFound() {
+ when(repository.findAll()).thenReturn(null);
+
+ assertNull(service.findAll());
+ verify(repository, atLeastOnce()).findAll();
+ }
}