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

avance de measure #9

Open
wants to merge 1 commit 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
Expand Up @@ -74,4 +74,9 @@ public List<Measure> getDeviceMeasures(
return measureService.getMeasuresByDevice(deviceId);
}

@GetMapping("/getbyLocation")
public List<Device> getLocationsDevices(@RequestParam(name="id_location") int id_location) {
return deviceService.getById_location(id_location);
}

}
Original file line number Diff line number Diff line change
@@ -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<Location> 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<Double> getMeasureValuesByLocation(@PathVariable("id_location") int id_location){
return locationService.getValueMeasure(id_location);
}

@GetMapping("/{id_location}/measures")
public List<Measure> getMeasuresByLocation(@PathVariable("id_location") int id_location){
return locationService.getMeasures(id_location);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ public interface IDevicesRepository extends CrudRepository<Device, Long> {
//findBy{{nombreAtributo}}(tipoAtributo {{nombreAtributo}}
List<Device> findByBranch(String branch);


List<Device> findByIdLocation(int id_location);

//consultas JPQL
//SELECT obj FROM Class obj WHERE {{predicados}},
// los predicados son en base a los atributos de la clase
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Location> {
@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<Location> 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<Location> 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<Double> 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<Measure> 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<Location> getLocation(){
return template.query(
"SELECT id_location, name, created_at, updated_at FROM tb_locations",
new LocationRowMapper());
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,7 @@ public void createDevice(Device device) {
public List<Device> getBytBranch(String branch) {
return devicesRepository.findByBranch(branch);
}
public List<Device> getById_location(int id_location){
return devicesRepository.findByIdLocation(id_location);
}
}
Original file line number Diff line number Diff line change
@@ -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<Location> 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<Double> getValueMeasure(int id_location){
return locationRepository.getValueMeasure(id_location);
}

public List<Measure> getMeasures(int id_location){
return locationRepository.getMeasures(id_location);
}


}
Original file line number Diff line number Diff line change
@@ -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);
Original file line number Diff line number Diff line change
@@ -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);