Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
import br.com.clickbus.challenge.exception.PlaceNotFoundException;
import br.com.clickbus.challenge.service.PlaceService;
import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.query.Param;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
Expand All @@ -18,13 +20,15 @@
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

@Api("places")
@RestController
@RequestMapping("places")
@RequiredArgsConstructor
public class PlaceController {

private PlaceService service;
private final PlaceService service;

@PostMapping
public ResponseEntity create(@RequestBody @Valid PlaceDTO dto) {
Expand All @@ -39,9 +43,14 @@ public ResponseEntity findById(@PathVariable Long id) {
}

@GetMapping
public ResponseEntity findAll() {
Iterable<PlaceDTO> places = PlaceDTO.convertToList(service.findAll());
return ResponseEntity.ok(places);
public ResponseEntity findAll(@Param("name") String name) {
List<Place> places = name != null ? service.findByName(name) : service.findAll();

if (places.isEmpty()) {
return ResponseEntity.notFound().build();
}

return ResponseEntity.ok(PlaceDTO.convertToList(places));
}

@PutMapping("/{id}")
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/br/com/clickbus/challenge/entity/Place.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.UpdateTimestamp;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
Expand Down Expand Up @@ -35,8 +38,10 @@ public class Place {
@NotNull
private String state;

@CreationTimestamp
private LocalDateTime createdAt;

@UpdateTimestamp
private LocalDateTime updatedAt;

public Place(String name, String slug, String city, String state) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@
@Repository
public interface PlaceRepository extends JpaRepository<Place, Long> {
List<Place> findByName(String name);
Place updateById(Long id, Place place);
}
26 changes: 19 additions & 7 deletions src/main/java/br/com/clickbus/challenge/service/PlaceService.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
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
Expand All @@ -19,22 +19,34 @@ public class PlaceService {
private PlaceRepository repository;

public List<Place> findAll() {
throw new NotImplementedException("Metodo nao implementado");
return repository.findAll();
}

public Optional<Place> 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<Place> 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");
public Place alter(@NotNull Place place, @NotNull PlaceDTO placeDTO) {
Place updatePlace = new Place(
place.getId(),
placeDTO.getName(),
placeDTO.getSlug(),
place.getCity(),
placeDTO.getState(),
place.getCreatedAt(),
LocalDateTime.now()
);

repository.save(updatePlace);

return updatePlace;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,13 @@
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;
Expand Down Expand Up @@ -112,4 +110,14 @@ void whenAlterPlaceOk() {
assertEquals(place.getCreatedAt(), edited.getCreatedAt());
assertNotNull(edited.getUpdatedAt());
}

@Test
void whenFindAllOk() {
when(repository.findAll()).thenReturn(Collections.singletonList(place));

List<Place> actual = service.findAll();

assertEquals(1, actual.size());
verify(repository, atLeastOnce()).findAll();
}
}