Skip to content
This repository has been archived by the owner on May 21, 2024. It is now read-only.

Commit

Permalink
feat: ✨ add setings database connection and handle
Browse files Browse the repository at this point in the history
  • Loading branch information
seba1204 committed Jun 1, 2022
1 parent 9b9e800 commit 80ab445
Show file tree
Hide file tree
Showing 4 changed files with 211 additions and 7 deletions.
14 changes: 7 additions & 7 deletions src/main/java/fr/photonsquid/houseportal/Device.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,27 +70,27 @@ public void setType(String type) {
this.type = type;
}

public Device id(int id) {
public Settings id(int id) {
setId(id);
return this;
}

public Device name(String name) {
public Settings name(String name) {
setName(name);
return this;
}

public Device location(String location) {
public Settings location(String location) {
setLocation(location);
return this;
}

public Device status(String status) {
public Settings status(String status) {
setStatus(status);
return this;
}

public Device type(String type) {
public Settings type(String type) {
setType(type);
return this;
}
Expand All @@ -99,10 +99,10 @@ public Device type(String type) {
public boolean equals(Object o) {
if (o == this)
return true;
if (!(o instanceof Device)) {
if (!(o instanceof Settings)) {
return false;
}
Device device = (Device) o;
Settings device = (Settings) o;
return id == device.id && Objects.equals(name, device.name) && Objects.equals(location, device.location)
&& Objects.equals(status, device.status) && Objects.equals(type, device.type);
}
Expand Down
95 changes: 95 additions & 0 deletions src/main/java/fr/photonsquid/houseportal/Settings.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package fr.photonsquid.houseportal;

import java.io.Serializable;
import java.util.Objects;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "devices")
public class Settings implements Serializable {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;

private String theme, u_id;

public Settings() {
}

public Settings(int id, String theme, String u_id) {
this.id = id;
this.theme = theme;
this.u_id = u_id;
}

public int getId() {
return this.id;
}

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

public String getTheme() {
return this.theme;
}

public void setTheme(String theme) {
this.theme = theme;
}

public String getU_id() {
return this.u_id;
}

public void setU_id(String u_id) {
this.u_id = u_id;
}

public Settings id(int id) {
setId(id);
return this;
}

public Settings theme(String theme) {
setTheme(theme);
return this;
}

public Settings u_id(String u_id) {
setU_id(u_id);
return this;
}

@Override
public boolean equals(Object o) {
if (o == this)
return true;
if (!(o instanceof Settings)) {
return false;
}
Settings settings = (Settings) o;
return id == settings.id && Objects.equals(theme, settings.theme) && Objects.equals(u_id, settings.u_id);
}

@Override
public int hashCode() {
return Objects.hash(id, theme, u_id);
}

@Override
public String toString() {
return "{" +
" id='" + getId() + "'" +
", theme='" + getTheme() + "'" +
", u_id='" + getU_id() + "'" +
"}";
}

}
42 changes: 42 additions & 0 deletions src/main/java/fr/photonsquid/houseportal/controller/Login.java
Original file line number Diff line number Diff line change
Expand Up @@ -144,4 +144,46 @@ public Response getDeviceInfo(BearerCredentials cr, @PathParam("id") int deviceI
}
}

@GET
@Produces("application/json")
@Path("/device")
public Response getSettings(BearerCredentials cr) {
int id = session.checkSession(cr.getBearer());

if (id < 0) {
return Response.status(403).build();
}

try {
Query query = em.createQuery("FROM Settings s WHERE u.u_id = :id");
query.setParameter("id", id);
return Response.ok(query.getResultList(), MediaType.APPLICATION_JSON).build();

} catch (NoResultException e) {
return Response.status(403).build();
}
}

@GET
@Produces("application/json")
@Path("/device/{id}")
public Response setUserSettings(SettingsCredentials cr, @PathParam("key") int key) {
int id = session.checkSession(cr.getBearer());

if (id < 0) {
return Response.status(403).build();
}

try {
// update user settings in database
Query query = em.createQuery("Update Device d Set u." + key + " = :value WHERE d.u_id = :id");
query.setParameter("id", id);
query.setParameter("value", cr.getValue());
return Response.ok(query.getSingleResult(), MediaType.APPLICATION_JSON).build();

} catch (NoResultException e) {
return Response.status(403).build();
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package fr.photonsquid.houseportal.controller;

import java.util.Objects;

public class SettingsCredentials {
private String bearer;
private String value;

public SettingsCredentials() {
}

public SettingsCredentials(String bearer, String value) {
this.bearer = bearer;
this.value = value;
}

public String getBearer() {
return this.bearer;
}

public void setBearer(String bearer) {
this.bearer = bearer;
}

public String getValue() {
return this.value;
}

public void setValue(String value) {
this.value = value;
}

public SettingsCredentials bearer(String bearer) {
setBearer(bearer);
return this;
}

public SettingsCredentials value(String value) {
setValue(value);
return this;
}

@Override
public boolean equals(Object o) {
if (o == this)
return true;
if (!(o instanceof SettingsCredentials)) {
return false;
}
SettingsCredentials settingsCredentials = (SettingsCredentials) o;
return Objects.equals(bearer, settingsCredentials.bearer) && Objects.equals(value, settingsCredentials.value);
}

@Override
public int hashCode() {
return Objects.hash(bearer, value);
}

@Override
public String toString() {
return "{" +
" bearer='" + getBearer() + "'" +
", value='" + getValue() + "'" +
"}";
}

}

0 comments on commit 80ab445

Please sign in to comment.