diff --git a/src/main/java/com/indramakers/example/measuresms/controllers/DeviceController.java b/src/main/java/com/indramakers/example/measuresms/controllers/DeviceController.java index bab33f4..821a927 100644 --- a/src/main/java/com/indramakers/example/measuresms/controllers/DeviceController.java +++ b/src/main/java/com/indramakers/example/measuresms/controllers/DeviceController.java @@ -74,4 +74,9 @@ public List getDeviceMeasures( return measureService.getMeasuresByDevice(deviceId); } + @GetMapping("/getbyLocation") + public List getLocationsDevices(@RequestParam(name="id_location") int id_location) { + return deviceService.getById_location(id_location); + } + } diff --git a/src/main/java/com/indramakers/example/measuresms/controllers/LocationController.java b/src/main/java/com/indramakers/example/measuresms/controllers/LocationController.java new file mode 100644 index 0000000..c37fe03 --- /dev/null +++ b/src/main/java/com/indramakers/example/measuresms/controllers/LocationController.java @@ -0,0 +1,44 @@ +package com.indramakers.example.measuresms.controllers; + +import com.indramakers.example.measuresms.model.entities.Location; +import com.indramakers.example.measuresms.model.entities.Measure; +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("/locations") +public class LocationController { + + @Autowired + private LocationService locationService; + + + @PostMapping + public void createLocation(@RequestBody Location location){ + locationService.createLocation(location); + } + + @GetMapping("/by_id") + public List getLocation(@RequestParam(name="id_location") int id){ + return locationService.getById(id); + } + + @DeleteMapping("/delete/{id_location}") + public void deleteLocation(@PathVariable("id_location") int id_location){ + locationService.deleteLocation(id_location); + } + + @GetMapping("/{id_location}/measures_value") + public List getMeasureValuesByLocation(@PathVariable("id_location") int id_location){ + return locationService.getValueMeasure(id_location); + } + + @GetMapping("/{id_location}/measures") + public List getMeasuresByLocation(@PathVariable("id_location") int id_location){ + return locationService.getMeasures(id_location); + } + +} diff --git a/src/main/java/com/indramakers/example/measuresms/model/entities/Device.java b/src/main/java/com/indramakers/example/measuresms/model/entities/Device.java index 5ece9bb..2a09a5a 100644 --- a/src/main/java/com/indramakers/example/measuresms/model/entities/Device.java +++ b/src/main/java/com/indramakers/example/measuresms/model/entities/Device.java @@ -38,21 +38,26 @@ public class Device implements Serializable { @Column(name = "updated_at") private Date updatedAt; + @Column(name = "id_location") + private int 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, Date createdAt, int id_location) { this.id = id; this.name = name; this.branch = branch; this.units = units; + this.idLocation = id_location; this.createdAt = createdAt; } - public Device(String name, String branch, String units) { + public Device(String name, String branch, String units, Long id_location) { this.name = name; this.branch = branch; this.units = units; + this.idLocation = id_location.intValue(); createdAt = new Date(); } @@ -87,4 +92,12 @@ public String getUnits() { public void setUnits(String units) { this.units = units; } + + public int getId_location() { + return idLocation; + } + + public void setId_location(int id_location) { + this.idLocation = id_location; + } } diff --git a/src/main/java/com/indramakers/example/measuresms/model/entities/Location.java b/src/main/java/com/indramakers/example/measuresms/model/entities/Location.java new file mode 100644 index 0000000..4d23cc3 --- /dev/null +++ b/src/main/java/com/indramakers/example/measuresms/model/entities/Location.java @@ -0,0 +1,52 @@ +package com.indramakers.example.measuresms.model.entities; + +import java.io.Serializable; +import java.util.Date; + +public class Location implements Serializable { + + private Long id_location; + private String name; + private Date created_at; + private Date updated_at; + + public Location() { + } + + public Location(String name) { + this.name = name; + } + + public Long getId_location() { + return id_location; + } + + public void setId_location(Long id_location) { + this.id_location = id_location; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Date getCreated_at() { + return created_at; + } + + public void setCreated_at(Date created_at) { + this.created_at = created_at; + } + + public Date getUpdated_at() { + return updated_at; + } + + public void setUpdated_at(Date updated_at) { + this.updated_at = updated_at; + } + +} diff --git a/src/main/java/com/indramakers/example/measuresms/repositories/IDevicesRepository.java b/src/main/java/com/indramakers/example/measuresms/repositories/IDevicesRepository.java index 13f07dd..ebee1fe 100644 --- a/src/main/java/com/indramakers/example/measuresms/repositories/IDevicesRepository.java +++ b/src/main/java/com/indramakers/example/measuresms/repositories/IDevicesRepository.java @@ -14,6 +14,9 @@ public interface IDevicesRepository extends CrudRepository { //findBy{{nombreAtributo}}(tipoAtributo {{nombreAtributo}} List findByBranch(String branch); + + List findByIdLocation(int id_location); + //consultas JPQL //SELECT obj FROM Class obj WHERE {{predicados}}, // los predicados son en base a los atributos de la clase diff --git a/src/main/java/com/indramakers/example/measuresms/repositories/LocationRepository.java b/src/main/java/com/indramakers/example/measuresms/repositories/LocationRepository.java new file mode 100644 index 0000000..2f7c93e --- /dev/null +++ b/src/main/java/com/indramakers/example/measuresms/repositories/LocationRepository.java @@ -0,0 +1,83 @@ +package com.indramakers.example.measuresms.repositories; + +import com.indramakers.example.measuresms.model.entities.Location; +import com.indramakers.example.measuresms.model.entities.Measure; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.jdbc.core.RowMapper; +import org.springframework.stereotype.Repository; + +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Timestamp; +import java.util.Date; +import java.util.List; + +class LocationRowMapper implements RowMapper { + @Override + public Location mapRow(ResultSet rs, int rowNum) throws SQLException { + Location location = new Location(); + location.setId_location(rs.getLong("id_location")); + location.setName(rs.getString("name")); + location.setCreated_at(rs.getDate("created_at")); + location.setUpdated_at(rs.getDate("updated_at")); + return location; + } +} + +@Repository +public class LocationRepository { + + + @Autowired + private JdbcTemplate template; + + public void createLocation(Location location) { + Date date = new Date(); + + template.update("INSERT INTO tb_locations(name, created_at) values(?,?)", + location.getName(), new Timestamp(date.getTime())); + } + + public List findByLocationId(int id_location) { + return template.query( + "SELECT id_location, name, created_at, updated_at FROM tb_locations WHERE id_location=?", + new LocationRowMapper() , + id_location); + } + + public List findByLocationName(String name) { + return template.query( + "SELECT id_location, name, created_at, updated_at FROM tb_locations WHERE name=?", + new LocationRowMapper() , + name); + } + + public void deleteLocation(int id_location){ + template.update("DELETE FROM tb_locations WHERE id_location = ?", + id_location); + } + + public List getValueMeasure(int id_location){ + return template.query( + "SELECT tb_measures.value FROM tb_measures JOIN tb_devices ON tb_measures.device_id = tb_devices.id_device JOIN tb_locations ON tb_devices.id_location = tb_locations.id_location WHERE tb_locations.id_location = ?", + (rs, rowNum) -> + rs.getDouble("value"), + id_location); + } + + public List getMeasures(int id_location){ + return template.query( + "SELECT tb_measures.value, tb_measures.date_time, tb_measures.device_id, tb_measures.id FROM tb_measures JOIN tb_devices ON tb_measures.device_id = tb_devices.id_device JOIN tb_locations ON tb_devices.id_location = tb_locations.id_location WHERE tb_locations.id_location = ?", + new MeasureRowMapper(), + id_location); + } + + public List getLocation(){ + return template.query( + "SELECT id_location, name, created_at, updated_at FROM tb_locations", + new LocationRowMapper()); + } + + } + diff --git a/src/main/java/com/indramakers/example/measuresms/services/DeviceService.java b/src/main/java/com/indramakers/example/measuresms/services/DeviceService.java index 2cd3ef9..6c5d562 100644 --- a/src/main/java/com/indramakers/example/measuresms/services/DeviceService.java +++ b/src/main/java/com/indramakers/example/measuresms/services/DeviceService.java @@ -37,4 +37,7 @@ public void createDevice(Device device) { public List getBytBranch(String branch) { return devicesRepository.findByBranch(branch); } + public List getById_location(int id_location){ + return devicesRepository.findByIdLocation(id_location); + } } diff --git a/src/main/java/com/indramakers/example/measuresms/services/LocationService.java b/src/main/java/com/indramakers/example/measuresms/services/LocationService.java new file mode 100644 index 0000000..b31455b --- /dev/null +++ b/src/main/java/com/indramakers/example/measuresms/services/LocationService.java @@ -0,0 +1,52 @@ +package com.indramakers.example.measuresms.services; + +import com.indramakers.example.measuresms.model.entities.Location; +import com.indramakers.example.measuresms.model.entities.Measure; +import com.indramakers.example.measuresms.repositories.IDevicesRepository; +import com.indramakers.example.measuresms.repositories.LocationRepository; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.List; + +@Service +public class LocationService { + + @Autowired + private LocationRepository locationRepository; + + @Autowired + private IDevicesRepository iDevicesRepository; + + public void createLocation(Location location){ + if(locationRepository.findByLocationName(location.getName()).isEmpty()){ + locationRepository.createLocation(location); + }else{ + throw new RuntimeException("Error, el nombre de location indicado ya existe"); + } + + } + + public List getById(int id){ + return locationRepository.findByLocationId(id); + } + + public void deleteLocation(int id_location){ + if (iDevicesRepository.findByIdLocation(id_location).isEmpty()){ + locationRepository.deleteLocation(id_location); + }else{ + throw new RuntimeException("Error, hay un device asociado a esa location"); + } + + } + + public List getValueMeasure(int id_location){ + return locationRepository.getValueMeasure(id_location); + } + + public List getMeasures(int id_location){ + return locationRepository.getMeasures(id_location); + } + + +} diff --git a/src/main/resources/db/migration/V1.4__create_location_table.sql b/src/main/resources/db/migration/V1.4__create_location_table.sql new file mode 100644 index 0000000..008d79b --- /dev/null +++ b/src/main/resources/db/migration/V1.4__create_location_table.sql @@ -0,0 +1,8 @@ +create table public.tb_locations ( + id_location serial primary key, + name varchar(255) NOT NULL, + created_at timestamp NOT NULL, + updated_at timestamp +); +ALTER TABLE tb_devices ADD id_location int NOT NULL; +ALTER TABLE tb_devices ADD CONSTRAINT fk_id_location FOREIGN KEY (id_location) REFERENCES tb_locations(id_location); diff --git a/src/main/resources/db/migration/V1.5__add_fk_table_measures.sql b/src/main/resources/db/migration/V1.5__add_fk_table_measures.sql new file mode 100644 index 0000000..1926568 --- /dev/null +++ b/src/main/resources/db/migration/V1.5__add_fk_table_measures.sql @@ -0,0 +1,2 @@ +ALTER TABLE tb_measures ALTER COLUMN device_id TYPE int USING device_id::integer; +ALTER TABLE tb_measures ADD CONSTRAINT fk_id_device FOREIGN KEY (device_id) REFERENCES tb_devices(id_device); \ No newline at end of file