Skip to content

Commit

Permalink
feat: added REST-Endpoint for Contracts with support for HTTP-GET Req…
Browse files Browse the repository at this point in the history
…uest to get all Contracts
  • Loading branch information
fabian-urner committed Mar 12, 2024
1 parent a62ed86 commit 549f074
Show file tree
Hide file tree
Showing 12 changed files with 280 additions and 160 deletions.
13 changes: 13 additions & 0 deletions src/main/java/dev/urner/volodb/dao/ContractDAO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package dev.urner.volodb.dao;

import java.util.List;

import dev.urner.volodb.entity.Contract;

public interface ContractDAO {
List<Contract> findAll();

Contract findById(int contractId);

List<Contract> findByVolunteerId(int volunteerId);
}
41 changes: 41 additions & 0 deletions src/main/java/dev/urner/volodb/dao/ContractDAOJpaImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package dev.urner.volodb.dao;

import java.util.List;

import org.springframework.stereotype.Repository;

import dev.urner.volodb.entity.Contract;
import jakarta.persistence.EntityManager;
import jakarta.persistence.TypedQuery;
import lombok.RequiredArgsConstructor;

@Repository
@RequiredArgsConstructor
public class ContractDAOJpaImpl implements ContractDAO {

private final EntityManager entityManager;

@Override
public List<Contract> findAll() {
TypedQuery<Contract> query = entityManager.createQuery("from Contract", Contract.class);
return query.getResultList();
}

@Override
public Contract findById(int contractId) {
return entityManager.find(Contract.class, contractId);
}

@Override
public List<Contract> findByVolunteerId(int volunteerId) {
TypedQuery<Contract> query = entityManager
.createQuery(
"SELECT c " +
"FROM Contract c " +
"WHERE c.volunteer.id = :volunteerId",
Contract.class)
.setParameter("volunteerId", volunteerId);
return query.getResultList();
}

}
34 changes: 34 additions & 0 deletions src/main/java/dev/urner/volodb/entity/ContactPerson.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package dev.urner.volodb.entity;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.Table;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.Setter;

@Entity
@Table(name = "contactPerson")
@Getter
@Setter
@RequiredArgsConstructor
public class ContactPerson {

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

@ManyToOne
@JoinColumn(name = "person")
private Person person;

@ManyToOne
@JoinColumn(name = "project")
private Project project;
}
168 changes: 42 additions & 126 deletions src/main/java/dev/urner/volodb/entity/Contract.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,64 @@
import java.time.LocalDateTime;
import java.util.Map;

import com.fasterxml.jackson.annotation.JsonAlias;
import com.fasterxml.jackson.annotation.JsonIdentityInfo;
import com.fasterxml.jackson.annotation.JsonIdentityReference;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;

import dev.urner.volodb.converter.HashMapConverter;
import jakarta.persistence.*;

import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.Setter;

@Entity
@Table(name="contract")

@Table(name = "contract")
@Getter
@Setter
@RequiredArgsConstructor
@JsonPropertyOrder({ "id", "created", "volunteerId", "projectId", "program", "start", "end", "visaNecessary",
"incomingVolunteer", "contactPerson", "holiday", "salary", "metadata" })
public class Contract {

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

@Column(name = "timestamp")
@JsonProperty("created")
private LocalDateTime dateOfCreation;

@ManyToOne
@JoinColumn(name = "volunteer") //FK
@JoinColumn(name = "volunteer") // FK
@JsonProperty("volunteerId")
@JsonAlias({ "volunteerId", "volunteer" })
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
@JsonIdentityReference(alwaysAsId = true)
private Volunteer volunteer;

@ManyToOne
@JoinColumn(name = "program") //FK
@JoinColumn(name = "program") // FK
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "shorthand")
@JsonIdentityReference(alwaysAsId = true)
private Program program;

@Column(name = "project") //FK
private int project;
@ManyToOne
@JsonProperty("projectId")
@JsonAlias({ "projectId", "project" })
@JoinColumn(name = "project") // FK
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
@JsonIdentityReference(alwaysAsId = true)
private Project project;

@Column(name = "contactPersonOfProject") //FK
private int contactPerson;
@ManyToOne
@JoinColumn(name = "contactPersonOfProject") // FK
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
@JsonIdentityReference(alwaysAsId = true)
private ContactPerson contactPerson;

@Column(name = "start")
private LocalDate start;
Expand All @@ -47,126 +75,14 @@ public class Contract {
@Column(name = "incomingVolunteer")
private boolean incomingVolunteer;

@Column(name = "salary") //FK
private int salary;
@ManyToOne
@JoinColumn(name = "salary") // FK
private Salary salary;

@Column(name = "holiday")
private int holiday;

@Convert(converter = HashMapConverter.class)
@Column(name = "metadata") //ToDo: JSON-Abhängigkeit
@Column(name = "metadata") // ToDo: JSON-Abhängigkeit
private Map<String, Object> metadata;



// define getter/setter

public int getId() {
return id;
}

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

public LocalDateTime getDateOfCreation() {
return dateOfCreation;
}

public void setDateOfCreation(LocalDateTime dateOfCreation) {
this.dateOfCreation = dateOfCreation;
}

public Volunteer getVolunteer() {
return volunteer;
}

public void setVolunteer(Volunteer volunteer) {
this.volunteer = volunteer;
}

public Program getProgram() {
return program;
}

public void setProgram(Program program) {
this.program = program;
}

public int getProject() {
return project;
}

public void setProject(int project) {
this.project = project;
}

public int getContactPerson() {
return contactPerson;
}

public void setContactPerson(int contactPerson) {
this.contactPerson = contactPerson;
}

public LocalDate getStart() {
return start;
}

public void setStart(LocalDate start) {
this.start = start;
}

public LocalDate getEnd() {
return end;
}

public void setEnd(LocalDate end) {
this.end = end;
}

public boolean isVisaNecessary() {
return visaNecessary;
}

public void setVisaNecessary(boolean visaNecessary) {
this.visaNecessary = visaNecessary;
}

public boolean isIncomingVolunteer() {
return incomingVolunteer;
}

public void setIncomingVolunteer(boolean incomingVolunteer) {
this.incomingVolunteer = incomingVolunteer;
}

public int getSalary() {
return salary;
}

public void setSalary(int salary) {
this.salary = salary;
}

public int getHoliday() {
return holiday;
}

public void setHoliday(int holiday) {
this.holiday = holiday;
}

public Map<String, Object> getMetadata() {
return metadata;
}

public void setMetadata(Map<String, Object> metadata) {
this.metadata = metadata;
}





}
31 changes: 5 additions & 26 deletions src/main/java/dev/urner/volodb/entity/Program.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package dev.urner.volodb.entity;

import jakarta.persistence.*;
import lombok.Getter;
import lombok.Setter;

@Entity
@Table(name = "program")
@Getter
@Setter
public class Program {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
Expand All @@ -17,29 +21,4 @@ public class Program {
@Column(name = "shorthand")
private String shorthand;


// define getter/setter
public int getId() {
return id;
}

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

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getShorthand() {
return shorthand;
}

public void setShorthand(String shorthand) {
this.shorthand = shorthand;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,6 @@ public Project deserialize(JsonParser jp, DeserializationContext ctxt) throws IO
myProject.setEmail(node.get("email").asText());

// Return Project

return myProject;

}
Expand Down
Loading

0 comments on commit 549f074

Please sign in to comment.