Skip to content

Commit 8a2c9b1

Browse files
committed
Creados la clase para historial de pesos (RecyclerView)
1 parent 41cdff3 commit 8a2c9b1

File tree

6 files changed

+196
-7
lines changed

6 files changed

+196
-7
lines changed

docker-compose.yml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ version: '3.8'
22
services:
33
mysql:
44
image: mysql:latest
5-
container_name: mysql_db
5+
container_name: databaseUsers
66
environment:
7-
MYSQL_ROOT_PASSWORD: 1234
8-
MYSQL_DATABASE: usersdb
7+
MYSQL_ROOT_PASSWORD: 123456
8+
MYSQL_DATABASE: users
99
MYSQL_USER: admin
10-
MYSQL_PASSWORD: admin
10+
MYSQL_PASSWORD: 1234
1111
ports:
12-
- "3306:3306"
12+
- "3308:3306"
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.kalories.demo.controller;
2+
3+
import java.util.List;
4+
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.web.bind.annotation.DeleteMapping;
7+
import org.springframework.web.bind.annotation.GetMapping;
8+
import org.springframework.web.bind.annotation.PostMapping;
9+
import org.springframework.web.bind.annotation.RequestBody;
10+
import org.springframework.web.bind.annotation.RequestMapping;
11+
import org.springframework.web.bind.annotation.RequestParam;
12+
import org.springframework.web.bind.annotation.RestController;
13+
14+
import com.kalories.demo.model.HistorialPeso;
15+
import com.kalories.demo.service.HistorialPesoService;
16+
17+
@RestController
18+
@RequestMapping("/api/historial-peso")
19+
public class HistorialPesoController {
20+
21+
@Autowired
22+
HistorialPesoService hps;
23+
24+
@GetMapping("/findAll")
25+
public List<HistorialPeso> findAll() {
26+
return hps.findAll();
27+
}
28+
29+
@GetMapping("/findByFecha")
30+
public List<HistorialPeso> findByDate(@RequestParam String fecha) {
31+
return hps.findByDate(fecha);
32+
}
33+
34+
@GetMapping("/findByPeso")
35+
public List<HistorialPeso> findByPeso(@RequestParam int peso) {
36+
return hps.findByWeight(peso);
37+
}
38+
39+
@PostMapping("/create")
40+
public int create(@RequestParam String fecha, @RequestParam int peso) {
41+
return hps.save(new HistorialPeso(fecha, peso));
42+
}
43+
44+
@DeleteMapping("/delete")
45+
public int delete(@RequestBody HistorialPeso hp) {
46+
return hps.delete(hp);
47+
}
48+
49+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.kalories.demo.model;
2+
3+
public class HistorialPeso {
4+
private Long id;
5+
private String fecha;
6+
private Integer peso;
7+
8+
public HistorialPeso() {
9+
}
10+
11+
public HistorialPeso(Long id, String fecha, Integer peso) {
12+
this.id = id;
13+
this.fecha = fecha;
14+
this.peso = peso;
15+
}
16+
17+
public HistorialPeso(String fecha, Integer peso) {
18+
this.fecha = fecha;
19+
this.peso = peso;
20+
}
21+
22+
public Long getId() {
23+
return id;
24+
}
25+
26+
public void setId(Long id) {
27+
this.id = id;
28+
}
29+
30+
public String getFecha() {
31+
return fecha;
32+
}
33+
34+
public void setFecha(String fecha) {
35+
this.fecha = fecha;
36+
}
37+
38+
public Integer getPeso() {
39+
return peso;
40+
}
41+
42+
public void setPeso(Integer peso) {
43+
this.peso = peso;
44+
}
45+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.kalories.demo.repository;
2+
3+
import java.sql.ResultSet;
4+
import java.sql.SQLException;
5+
import java.util.List;
6+
7+
import org.springframework.beans.factory.annotation.Autowired;
8+
import org.springframework.jdbc.core.JdbcTemplate;
9+
import org.springframework.jdbc.core.RowMapper;
10+
import org.springframework.stereotype.Repository;
11+
12+
import com.kalories.demo.model.HistorialPeso;
13+
14+
@Repository
15+
public class HistorialPesoRepository {
16+
@Autowired
17+
JdbcTemplate jdbcTemplate;
18+
19+
private static final class HistorialPesoRowMapper implements RowMapper<HistorialPeso> {
20+
21+
@Override
22+
public HistorialPeso mapRow(ResultSet rs, int rowNum) throws SQLException {
23+
return new HistorialPeso(
24+
rs.getLong("id"),
25+
rs.getString("fecha"),
26+
rs.getInt("peso"));
27+
}
28+
}
29+
30+
public List<HistorialPeso> findAll() {
31+
String sql = "SELECT * FROM historial_pesos";
32+
return jdbcTemplate.query(sql, new HistorialPesoRowMapper());
33+
}
34+
35+
public List<HistorialPeso> findByWeight(int peso) {
36+
String sql = "SELECT * FROM historial_pesos WHERE peso = ?";
37+
return jdbcTemplate.query(sql, new HistorialPesoRowMapper(), peso);
38+
}
39+
40+
public List<HistorialPeso> findByDate(String fecha) {
41+
String sql = "SELECT * FROM historial_pesos WHERE FECHA = ?";
42+
return jdbcTemplate.query(sql, new HistorialPesoRowMapper(), fecha);
43+
}
44+
45+
public int save(HistorialPeso historialPeso) {
46+
String sql = "INSERT INTO historial_pesos (FECHA, PESO) VALUES (?, ?)";
47+
return jdbcTemplate.update(sql, historialPeso.getFecha(), historialPeso.getPeso());
48+
}
49+
50+
public int delete(HistorialPeso historialPeso) {
51+
String sql = "DELETE FROM historial_pesos WHERE FECHA = ? AND PESO = ?";
52+
return jdbcTemplate.update(sql, historialPeso.getFecha(), historialPeso.getPeso());
53+
}
54+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.kalories.demo.service;
2+
3+
import java.util.List;
4+
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.stereotype.Service;
7+
8+
import com.kalories.demo.model.HistorialPeso;
9+
import com.kalories.demo.repository.HistorialPesoRepository;
10+
11+
@Service
12+
public class HistorialPesoService {
13+
14+
@Autowired
15+
HistorialPesoRepository hpr;
16+
17+
public List<HistorialPeso> findAll() {
18+
return hpr.findAll();
19+
}
20+
21+
public List<HistorialPeso> findByWeight(int peso) {
22+
return hpr.findByWeight(peso);
23+
}
24+
25+
public List<HistorialPeso> findByDate(String fecha) {
26+
return hpr.findByDate(fecha);
27+
}
28+
29+
public int save(HistorialPeso historialPeso) {
30+
return hpr.save(historialPeso);
31+
}
32+
33+
public int delete(HistorialPeso historialPeso) {
34+
return hpr.delete(historialPeso);
35+
}
36+
}

src/main/resources/schema.sql

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
DROP TABLE IF EXISTS users;
2-
CREATE TABLE users (
1+
CREATE TABLE IF NOT EXISTS users (
32
id BIGINT AUTO_INCREMENT PRIMARY KEY,
43
email VARCHAR(255) NOT NULL UNIQUE,
54
contrasena VARCHAR(255) NOT NULL,
@@ -10,4 +9,10 @@ CREATE TABLE users (
109
nivel_actividad TINYINT CHECK (nivel_actividad IN (0, 1, 2)),
1110
objetivo TINYINT CHECK (objetivo in (0, 1, 2)),
1211
img_path varchar(255)
12+
);
13+
14+
CREATE TABLE IF NOT EXISTS historial_pesos(
15+
id BIGINT AUTO_INCREMENT PRIMARY KEY,
16+
fecha VARCHAR(11),
17+
peso INTEGER
1318
);

0 commit comments

Comments
 (0)