Skip to content
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

Funcionalidad Location #5

Open
wants to merge 2 commits into
base: main
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.indramakers.example.measuresms.config;

public class Config {

public static final Double MIN_MEAUSURE_VALUE = 0.0;

public static final Double MAX_MEAUSURE_VALUE = 100.0;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.indramakers.example.measuresms.config;

public enum ErrorCodes {
DEVICE_WITH_NAME_EXISTS("Device with that name already exists", "001"),
MEASURE_VALUES_OUT_OF_RANGE("Measures must be in range", "002"),
DEVICE_NOT_FOUND("Device not found", "003"),
INVALID_ID_BRANCH("Invalid branch", "004"),
INVALID_ID_LOCATION("Invalid IdLocation", "005"),

ID_LOCATION_NOT_EXIST("IdLocation not exist", "006"),
INVALID_ID_DEVICE("Invalid IdDevice", "007");

String message;
String code;

ErrorCodes(String message, String code) {
this.message = message;
this.code = code;
}

public String getMessage() {
return message;
}

public String getCode() {
return code;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.indramakers.example.measuresms.config;

public class Routes {

public static final String DEVICES_PATH = "/devices";

public static final String DEVICE_BY_BRANCH_PATH = "/by-branch";

public static final String MEASURES_BY_DEVICE_PATH = "/{deviceId}/measures";
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package com.indramakers.example.measuresms.controllers;

import com.indramakers.example.measuresms.model.entities.Device;
import com.indramakers.example.measuresms.model.entities.Location;
import com.indramakers.example.measuresms.model.entities.Measure;
import com.indramakers.example.measuresms.model.requests.MeasureValueRequest;
import com.indramakers.example.measuresms.services.DeviceService;
import com.indramakers.example.measuresms.services.LocationService;
import com.indramakers.example.measuresms.services.MeasureService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
Expand All @@ -21,6 +23,9 @@ public class DeviceController {
@Autowired
private MeasureService measureService;

@Autowired
private LocationService locationService;

/**
* URL /devices
* @param device
Expand Down Expand Up @@ -58,8 +63,7 @@ public List<Device> getDevicesByBranch2(@RequestParam(name = "branch") String br
@PostMapping("/{deviceId}/measures")
public void addMeasureToDevice(
@Valid @RequestBody MeasureValueRequest request,
@PathVariable("deviceId") String deviceId) {

@PathVariable("deviceId") Long deviceId) {
measureService.registerMeasure(deviceId, request.getValue());
}

Expand All @@ -69,9 +73,14 @@ public void addMeasureToDevice(

@GetMapping("/{deviceId}/measures")
public List<Measure> getDeviceMeasures(
@PathVariable("deviceId") String deviceId) {
@PathVariable("deviceId") Long deviceId) {

return measureService.getMeasuresByDevice(deviceId);
}

@GetMapping("/{idLocation}/locations")
public List<Device> getDeviceLocations(@PathVariable("idLocation") Long idLocation) {
return deviceService.getByIdLocation(idLocation);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.indramakers.example.measuresms.controllers;

import com.indramakers.example.measuresms.model.entities.Location;
import com.indramakers.example.measuresms.services.LocationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("/location")
public class LocationController {
@Autowired
private LocationService locationService;

@PostMapping
public void create(@RequestBody Location location) {
locationService.registerLocation(location.getId(), location.getName());
}

@GetMapping("/{id}")
public List<Location> getLocation(@PathVariable("id") Long id) {
return locationService.getLocationById(id);
}

@DeleteMapping("/{id}")
public void delete(@PathVariable("id") Long id) {
locationService.deleteLocationById(id);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.indramakers.example.measuresms.controllers;

import com.indramakers.example.measuresms.model.entities.Measure;
import com.indramakers.example.measuresms.services.MeasureService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/measures")
public class MeasuresController {

@Autowired
private MeasureService measureService;

@GetMapping("/{idLocation}/locations")
public List<Measure> getMeasuresByLocation (@PathVariable("idLocation") Long idLocation) {
return measureService.getMeasuresByLocation(idLocation);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.indramakers.example.measuresms.exceptions;

import com.indramakers.example.measuresms.config.ErrorCodes;

public class BusinessException extends RuntimeException {

private ErrorCodes code;

public BusinessException(String message) {
super(message);
}

public BusinessException(ErrorCodes code) {
super(code.getMessage());
this.code = code;
}

public String getCode() {
return code.getCode();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.indramakers.example.measuresms.exceptions;

import com.indramakers.example.measuresms.model.responses.ErrorResponse;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

@ControllerAdvice
public class CustomExceptionHandler {

@ResponseStatus(HttpStatus.PRECONDITION_FAILED)
@ResponseBody //que la respuesta va a ser personalizada.
@ExceptionHandler(BusinessException.class)
public ErrorResponse handleBusinessException(BusinessException exception) {
return new ErrorResponse(exception.getCode(), exception.getMessage());
}

@ResponseStatus(HttpStatus.NOT_FOUND)
@ResponseBody
@ExceptionHandler(NotFoundException.class)
public ErrorResponse handleNotFoundException(NotFoundException exception) {
return new ErrorResponse("BAD_PARAMETERS", exception.getMessage());
}

@ResponseStatus(HttpStatus.BAD_REQUEST)
@ResponseBody
@ExceptionHandler(MethodArgumentNotValidException.class)
public ErrorResponse handleNotFoundException(MethodArgumentNotValidException exception) {
return new ErrorResponse("NOT_FOUND", exception.getMessage());
}

@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
@ResponseBody
@ExceptionHandler(Exception.class)
public ErrorResponse handleException(Exception exception) {
return new ErrorResponse("500", exception.getMessage());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.indramakers.example.measuresms.exceptions;

public class NotFoundException extends RuntimeException{
public NotFoundException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,17 @@ public class Device implements Serializable {
private Long id;

@Column(name = "name_device")
@Pattern(regexp = "[A-Z]{3}-[0-9]{3}")
//@Pattern(regexp = "[A-Z]{3}-[0-9]{3}")
private String name;

@Column(name = "branch_device")
@NotBlank
@NotEmpty
@Length(min = 3, max = 20)
@Length(min = 1, max = 20)
private String branch;

@Column(name = "measure_unit")
@Pattern(regexp = "[A-Z]{3}")
//@Pattern(regexp = "[A-Z]{3}")
private String units;

@Column(name = "created_at")
Expand All @@ -38,22 +38,27 @@ public class Device implements Serializable {
@Column(name = "updated_at")
private Date updatedAt;

@Column(name = "id_location")
private Long idLocation;

public Device() {
}

public Device(Long id, String name, String branch, String units, Date createdAt) {
public Device(Long id, String name, String branch, String units, Long idLocation, Date createdAt) {
this.id = id;
this.name = name;
this.branch = branch;
this.units = units;
this.createdAt = createdAt;
this.idLocation = idLocation;
}

public Device(String name, String branch, String units) {
public Device(String name, String branch, String units, Long idLocation) {
this.name = name;
this.branch = branch;
this.units = units;
createdAt = new Date();
this.idLocation = idLocation;
}

public Long getId() {
Expand Down Expand Up @@ -87,4 +92,12 @@ public String getUnits() {
public void setUnits(String units) {
this.units = units;
}

public Long getIdLocation() {
return idLocation;
}

public void setIdLocation(Long idLocation) {
this.idLocation = idLocation;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.indramakers.example.measuresms.model.entities;

import javax.persistence.*;
import java.io.Serializable;

public class Location implements Serializable {

private Long id;

private String name;

public Location() {
}

public Location(Long id, String name) {
this.id = id;
this.name = name;
}

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
public class Measure implements Serializable {

private Long id;
private String deviceId;
private Long deviceId;
private Double value;
private Date dateTime;

public Measure() {
}

public Measure(String deviceId, Double value) {
public Measure(Long deviceId, Double value) {
this.deviceId = deviceId;
this.value = value;
}
Expand All @@ -26,11 +26,11 @@ public void setId(Long id) {
this.id = id;
}

public String getDeviceId() {
public Long getDeviceId() {
return deviceId;
}

public void setDeviceId(String deviceId) {
public void setDeviceId(Long deviceId) {
this.deviceId = deviceId;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.indramakers.example.measuresms.model.responses;

public class ErrorResponse {

private String code;
private String message;

public ErrorResponse() {
}

public ErrorResponse(String code, String message) {
this.code = code;
this.message = message;
}

public String getCode() {
return code;
}

public void setCode(String code) {
this.code = code;
}

public String getMessage() {
return message;
}

public void setMessage(String message) {
this.message = message;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,6 @@ public interface IDevicesRepository extends CrudRepository<Device, Long> {
// los predicados son en base a los atributos de la clase
@Query("SELECT dev FROM Device dev WHERE dev.name = :name")
List<Device> findByName(String name);

List<Device> findByIdLocation(Long idLocation);
}
Loading