Skip to content

Commit

Permalink
Changes Java 8 e mudanças no insert do mongo, para salvar na collecti…
Browse files Browse the repository at this point in the history
…on especifica da model
  • Loading branch information
emiteze committed Dec 7, 2017
1 parent f8c6925 commit cea59ee
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public ResponseEntity<?> getPointsNearby(@RequestParam("coordx") int coordx, @Re
}
}

@RequestMapping(value = "/save-point", method = RequestMethod.POST)
@RequestMapping(value = "/save-point", method = {RequestMethod.POST, RequestMethod.PUT})
public ResponseEntity<?> savePoint(@RequestBody PointOfInterest poi){
try {
poiService.savePoint(poi);
Expand All @@ -58,7 +58,7 @@ public ResponseEntity<?> savePoint(@RequestBody PointOfInterest poi){
}
}

@RequestMapping(value = "/delete-point", method = RequestMethod.DELETE)
@RequestMapping(value = "/delete-point", method = {RequestMethod.DELETE, RequestMethod.POST})
public ResponseEntity<?> deletePoint(@RequestParam(value = "id", required = false) UUID id, @RequestParam(value = "name", required = false) String name, @RequestBody(required = false) PointOfInterest poi){
try {
if(id != null){
Expand All @@ -77,20 +77,4 @@ public ResponseEntity<?> deletePoint(@RequestParam(value = "id", required = fals
return ResponseEntity.badRequest().build();
}

@RequestMapping(value = "/update-point", method = RequestMethod.PUT)
public ResponseEntity<?> updatePoint(@RequestBody PointOfInterest poi){
try {
poiService.updatePoint(poi);
return ResponseEntity.ok(poiService.getPoints());
} catch(Exception e){
return ResponseEntity.badRequest().build();
}
}

/*@RequestMapping(value = "/save-point", method = RequestMethod.POST)
public List<PointOfInterest> savePoint(@RequestParam("name") String name, @RequestParam("coordx") int coordx, @RequestParam("coordy") int coordy){
poiService.savePoint(new PointOfInterest(name, coordx, coordy));
return poiService.getPoints();
}*/

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@

import org.hibernate.validator.constraints.NotEmpty;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;

import javax.validation.constraints.Min;
import java.io.Serializable;
import java.util.UUID;

@Document(collection = "pointsofinterest")
public class PointOfInterest implements Serializable{

@Id
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
import org.springframework.stereotype.Service;
import zup.mateus.cruz.xyinc.repository.PointOfInterestRepository;
import zup.mateus.cruz.xyinc.model.PointOfInterest;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

@Service
public class PointOfInterestService {
Expand All @@ -19,10 +19,7 @@ public List<PointOfInterest> getPoints(){
}

public List<PointOfInterest> getPointsNearby(PointOfInterest userLocation, double maxDistance){
List<PointOfInterest> nearbyPoints = new ArrayList<>();
for (PointOfInterest poi : this.getPoints()) {
if(userLocation.distanceBetweenPoints(poi) <= maxDistance) nearbyPoints.add(poi);
}
List<PointOfInterest> nearbyPoints = this.getPoints().stream().filter(point -> userLocation.distanceBetweenPoints(point) <= maxDistance).collect(Collectors.toList());
return nearbyPoints;
}

Expand All @@ -36,9 +33,7 @@ public List<PointOfInterest> findPointsByName(String name){

public void deleteByName(String name){
List<PointOfInterest> pointsToBeRemoved = findPointsByName(name);
for(PointOfInterest poi : pointsToBeRemoved){
repository.delete(poi.getId());
}
repository.delete(pointsToBeRemoved);
}

public void deleteObject(PointOfInterest poi) { repository.delete(poi); }
Expand All @@ -51,6 +46,4 @@ public void savePoint(PointOfInterest poi){
repository.save(poi);
}

public void updatePoint(PointOfInterest poi){ repository.save(poi); }

}
Original file line number Diff line number Diff line change
Expand Up @@ -35,22 +35,22 @@ public void restoreDB() {
}

private void saveInRepoEachPointInList(List<PointOfInterest> listOfPoints){
for(PointOfInterest poi : listOfPoints){
repository.save(poi);
}
listOfPoints.forEach(point -> {
repository.save(point);
});
}

private void deleteInRepoEachPointInList(List<PointOfInterest> listOfPoints){
for(PointOfInterest poi : listOfPoints){
repository.delete(poi);
}
listOfPoints.forEach(point -> {
repository.delete(point);
});
}

@Test
public void testFindAllPopulatedDB() throws Exception {

List<PointOfInterest> backup = new ArrayList<>();
for(PointOfInterest poi : repository.findAll()) backup.add(poi);
backup.addAll(repository.findAll());
repository.deleteAll();

PointOfInterest pointOne = new PointOfInterest("point1", 10, 10);
Expand Down Expand Up @@ -109,7 +109,7 @@ public void testFindByNameReturnsListOfObject() throws Exception {
public void testDeleteByNonExistingId() throws Exception {

List<PointOfInterest> backup = new ArrayList<>();
for(PointOfInterest poi : repository.findAll()) backup.add(poi);
backup.addAll(repository.findAll());
repository.deleteAll();

PointOfInterest pointOne = new PointOfInterest("point1", 10, 10);
Expand All @@ -131,7 +131,7 @@ public void testDeleteByNonExistingId() throws Exception {
public void testDeleteByExistingId() throws Exception {

List<PointOfInterest> backup = new ArrayList<>();
for(PointOfInterest poi : repository.findAll()) backup.add(poi);
backup.addAll(repository.findAll());
repository.deleteAll();

PointOfInterest pointOne = new PointOfInterest("point1", 10, 10);
Expand Down

0 comments on commit cea59ee

Please sign in to comment.