Skip to content

Feat requisitos faltantes #5

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
10 changes: 9 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,15 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>9</source>
<target>9</target>
</configuration>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,27 @@
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
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()))
Expand All @@ -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));

}
}
33 changes: 33 additions & 0 deletions src/main/java/br/com/clickbus/challenge/dto/PlaceDTO.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,44 @@ public static PlaceDTO of(String name, String slug, String city, String state) {
return new PlaceDTO(name, slug, city, state);
}


public static Iterable<PlaceDTO> convertToList(List<Place> places) {
return places.stream().map(Place::convertToDTO).collect(Collectors.toList());
}

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;
}
}
60 changes: 60 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 @@ -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;
Expand All @@ -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;
}
}
22 changes: 16 additions & 6 deletions src/main/java/br/com/clickbus/challenge/service/PlaceService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<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");

place.setName(placeDTO.getName());
place.setSlug(placeDTO.getSlug());
place.setCity(place.getCity());
place.setState(placeDTO.getState());
place.setUpdatedAt(LocalDateTime.now());

return repository.save(place);
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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<Place> 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();
}
}