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

Add dataaccess layer #89

Merged
merged 1 commit into from
May 29, 2024
Merged
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
51 changes: 40 additions & 11 deletions web_backend/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
<checkstyle-plugin.version>3.3.1</checkstyle-plugin.version>
<checkstyle.version>10.13.0</checkstyle.version>
<compiler-plugin.version>3.12.1</compiler-plugin.version>
<jakarta.validation.version>3.0.2</jakarta.validation.version>
<jakarta.ws.rs.version>3.1.0</jakarta.ws.rs.version>
<lombok.version>1.18.32</lombok.version>
<maven.compiler.release>21</maven.compiler.release>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
Expand All @@ -17,7 +20,6 @@
<quarkus.platform.version>3.9.4</quarkus.platform.version>
<skipITs>true</skipITs>
<surefire-plugin.version>3.2.5</surefire-plugin.version>
<jakarta.ws.rs.version>3.1.0</jakarta.ws.rs.version>
</properties>
<dependencyManagement>
<dependencies>
Expand All @@ -39,20 +41,47 @@
<groupId>io.quarkus</groupId>
<artifactId>quarkus-rest-client-jackson</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-hibernate-orm-panache</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-jdbc-postgresql</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-jdbc-h2</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-junit5</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-test-h2</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>jakarta.ws.rs</groupId>
<artifactId>jakarta.ws.rs-api</artifactId>
<version>${jakarta.ws.rs.version}</version>
<groupId>jakarta.ws.rs</groupId>
<artifactId>jakarta.ws.rs-api</artifactId>
<version>${jakarta.ws.rs.version}</version>
</dependency>
<dependency>
<groupId>jakarta.validation</groupId>
<artifactId>jakarta.validation-api</artifactId>
<version>${jakarta.validation.version}</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
</dependency>
</dependencies>
<build>
Expand Down Expand Up @@ -134,6 +163,13 @@
<groupId>org.jsonschema2pojo</groupId>
<artifactId>jsonschema2pojo-maven-plugin</artifactId>
<version>1.2.1</version>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<configuration>
<sourceDirectory>src/main/resources/jsonschema/events</sourceDirectory>
<targetPackage>haw.teamagochi.backend.leshanclient.sse</targetPackage>
Expand All @@ -145,13 +181,6 @@
<includeGetters>false</includeGetters>
<includeHashcodeAndEquals>false</includeHashcodeAndEquals>
</configuration>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package haw.teamagochi.backend.device.dataaccess.model;
import io.quarkus.hibernate.orm.panache.PanacheEntity;
import jakarta.persistence.Embedded;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import jakarta.validation.constraints.Size;
import java.util.Objects;
import lombok.Getter;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import lombok.Setter;

@Getter
@Setter
@RequiredArgsConstructor
@Entity
public class DeviceEntity {

// oh nooo, I changed smt. Plz recompile

public DeviceEntity() {}

@Id
@GeneratedValue
private long id;

@NonNull
@Size(max = 255)
private String name;

@Embedded
@NonNull
private DeviceType deviceType;


@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
DeviceEntity device = (DeviceEntity) o;
return id == device.id && name.equals(device.name) && deviceType == device.deviceType;
}

@Override
public int hashCode() {
return Objects.hash(id, name, deviceType);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package haw.teamagochi.backend.device.dataaccess.model;

import jakarta.persistence.Embeddable;

@Embeddable
public enum DeviceType {
FROG;

DeviceType() {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package haw.teamagochi.backend.device.dataaccess.repository;

import haw.teamagochi.backend.device.dataaccess.model.DeviceEntity;
import io.quarkus.hibernate.orm.panache.PanacheRepository;
import jakarta.enterprise.context.ApplicationScoped;

@ApplicationScoped
public class DeviceRepository implements PanacheRepository<DeviceEntity> {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package haw.teamagochi.backend.device.logic;


import haw.teamagochi.backend.device.dataaccess.model.DeviceEntity;
import haw.teamagochi.backend.device.dataaccess.model.DeviceType;

public interface DeviceService {

/**
* Creates a device and saves it persistently in the database.
* Any future changes to the device object's attributes will be updated automatically in the database.
* @param name name of the device
* @param deviceType device type
* @return persisted device object
*/
DeviceEntity createDevice(String name, DeviceType deviceType);

/**
* Returns whether device exists in the database.
* @param id id of the device
* @return whether device was found
*/
boolean deviceExists(long id);

/**
* Deletes all devices from the database.
*/
void deleteAll();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package haw.teamagochi.backend.device.logic;

import haw.teamagochi.backend.device.dataaccess.model.DeviceEntity;
import haw.teamagochi.backend.device.dataaccess.model.DeviceType;
import haw.teamagochi.backend.device.dataaccess.repository.DeviceRepository;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;

@ApplicationScoped
public class DeviceServiceImpl implements DeviceService {

@Inject
DeviceRepository deviceRepository;

@Override
public DeviceEntity createDevice(String name, DeviceType deviceType) {
DeviceEntity device = new DeviceEntity(name, deviceType);
deviceRepository.persist(device);
return device;
}

@Override
public boolean deviceExists(long id) {
DeviceEntity device = deviceRepository.findById(id);
return device != null;
}

@Override
public void deleteAll() {
deviceRepository.deleteAll();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package haw.teamagochi.backend.pet.dataaccess.model;

import haw.teamagochi.backend.device.dataaccess.model.DeviceEntity;
import io.quarkus.hibernate.orm.panache.PanacheEntity;
import jakarta.annotation.Nullable;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.OneToOne;
import jakarta.validation.constraints.Past;
import jakarta.validation.constraints.Pattern;
import jakarta.validation.constraints.PositiveOrZero;
import jakarta.validation.constraints.Size;
import java.sql.Date;
import java.util.Objects;
import lombok.Getter;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import lombok.Setter;

@Getter
@Setter
@RequiredArgsConstructor
@Entity
public class PetEntity {

@Id
@GeneratedValue
private long id;

public PetEntity() {}


@NonNull
@Size(max = 255)
private String name;


/*
@NonNull
@Pattern(regexp = "^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$") // source: https://www.geeksforgeeks.org/how-to-validate-hexadecimal-color-code-using-regular-expression/
private String color;
*/

private int happiness = 0;
private int wellbeing = 0;
private int health = 0;
private int hunger = 0;
private int cleanliness = 0;
private int fun = 0;

@PositiveOrZero
private int xp = 0;

@Nullable
@Past
private Date lastTimeOnDevice;


@ManyToOne
@NonNull
private PetTypeEntity petType;


@OneToOne
@Nullable
private DeviceEntity device;


@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
PetEntity petEntity = (PetEntity) o;
return id == petEntity.id;
}

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



}

Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package haw.teamagochi.backend.pet.dataaccess.model;
import io.quarkus.hibernate.orm.panache.PanacheEntity;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import jakarta.validation.constraints.Size;
import lombok.Getter;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import lombok.Setter;

@Getter
@Setter
@RequiredArgsConstructor
@Entity
public class PetTypeEntity {

@Id
@GeneratedValue
private long id;

public PetTypeEntity() {}

@NonNull
@Size(max = 255)
private String name;


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package haw.teamagochi.backend.pet.dataaccess.repository;

import haw.teamagochi.backend.pet.dataaccess.model.PetEntity;
import io.quarkus.hibernate.orm.panache.PanacheRepository;
import jakarta.enterprise.context.ApplicationScoped;

@ApplicationScoped
public class PetRepository implements PanacheRepository<PetEntity> {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package haw.teamagochi.backend.pet.dataaccess.repository;


import haw.teamagochi.backend.pet.dataaccess.model.PetTypeEntity;
import io.quarkus.hibernate.orm.panache.PanacheRepository;
import jakarta.enterprise.context.ApplicationScoped;

@ApplicationScoped
public class PetTypeRepository implements PanacheRepository<PetTypeEntity> {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package haw.teamagochi.backend.pet.logic;

import haw.teamagochi.backend.pet.dataaccess.model.PetEntity;
import haw.teamagochi.backend.pet.dataaccess.model.PetTypeEntity;

public interface PetService {
/**
* Creates a pet and saves it persistently in the database.
* Any future changes to the pet object's attributes will be updated automatically in the database.
* @param name name for the pet
* @param petType pet type of the pet
* @return persisted pet object
*/
PetEntity createPet(String name, PetTypeEntity petType);
}
Loading