From 194b14f02b18b05ca586945a515a4a917fc562ee Mon Sep 17 00:00:00 2001
From: LeoMoulin <leomoulin125@gmail.com>
Date: Mon, 8 Apr 2024 10:18:28 +0200
Subject: [PATCH] Implements the backend logic for the minerval gestion

---
 .../Clyde/EndPoints/MinervalController.java   | 45 +++++++++++++
 .../Repositories/MinervalRepository.java      | 10 +++
 .../ovh/herisson/Clyde/Tables/Minerval.java   | 64 +++++++++++++++++++
 3 files changed, 119 insertions(+)
 create mode 100644 backend/src/main/java/ovh/herisson/Clyde/EndPoints/MinervalController.java
 create mode 100644 backend/src/main/java/ovh/herisson/Clyde/Repositories/MinervalRepository.java
 create mode 100644 backend/src/main/java/ovh/herisson/Clyde/Tables/Minerval.java

diff --git a/backend/src/main/java/ovh/herisson/Clyde/EndPoints/MinervalController.java b/backend/src/main/java/ovh/herisson/Clyde/EndPoints/MinervalController.java
new file mode 100644
index 0000000..7d93a04
--- /dev/null
+++ b/backend/src/main/java/ovh/herisson/Clyde/EndPoints/MinervalController.java
@@ -0,0 +1,45 @@
+package ovh.herisson.Clyde.EndPoints;
+
+import org.apache.coyote.Response;
+import org.springframework.http.HttpStatus;
+import org.springframework.http.ResponseEntity;
+import org.springframework.web.bind.annotation.*;
+import ovh.herisson.Clyde.Repositories.MinervalRepository;
+import ovh.herisson.Clyde.Responses.UnauthorizedResponse;
+import ovh.herisson.Clyde.Services.AuthenticatorService;
+import ovh.herisson.Clyde.Tables.Minerval;
+import ovh.herisson.Clyde.Tables.Role;
+
+import java.util.*;
+
+@RestController
+public class MinervalController {
+    private final AuthenticatorService authServ;
+    private final MinervalRepository mr;
+
+    public MinervalController(AuthenticatorService authServ, MinervalRepository mr) {
+        this.authServ = authServ;
+        this.mr = mr;
+    }
+
+    //A new minerval entry is posted when the inscription service accept a registration request
+    @PostMapping("/minerval/{studentRegNo}")
+    public ResponseEntity<Object> postMinerval(@RequestHeader("Authorization") String token, @PathVariable long studentRegNo){
+        if (authServ.isNotIn(new Role[]{Role.Admin,Role.Secretary,Role.InscriptionService},token))
+            return new UnauthorizedResponse<>(null);
+
+        Calendar c = new GregorianCalendar();
+
+        mr.save(new Minerval(studentRegNo, 0, 835, c.get(Calendar.YEAR)));
+        return new ResponseEntity<>(HttpStatus.OK);
+    }
+
+    @GetMapping("/minerval/{studentRegNo}")
+    public ResponseEntity<Minerval> getCurrentMinervalbyRegNo(@PathVariable long studentRegNo){
+        ArrayList<Minerval> mlist = mr.getMinervalsByStudentRegNoOrderByYearDesc(studentRegNo);
+
+        //The list is ordered by year in descending order then the index 0 contains the actual minerval (for this year)
+        Minerval m = mlist.get(0);
+        return new ResponseEntity<>(m, HttpStatus.OK);
+    }
+}
diff --git a/backend/src/main/java/ovh/herisson/Clyde/Repositories/MinervalRepository.java b/backend/src/main/java/ovh/herisson/Clyde/Repositories/MinervalRepository.java
new file mode 100644
index 0000000..37c46d7
--- /dev/null
+++ b/backend/src/main/java/ovh/herisson/Clyde/Repositories/MinervalRepository.java
@@ -0,0 +1,10 @@
+package ovh.herisson.Clyde.Repositories;
+
+import org.springframework.data.repository.CrudRepository;
+import ovh.herisson.Clyde.Tables.Minerval;
+
+import java.util.ArrayList;
+
+public interface MinervalRepository extends CrudRepository<Minerval, Long> {
+    public ArrayList<Minerval> getMinervalsByStudentRegNoOrderByYearDesc(Long studentRegNo);
+}
diff --git a/backend/src/main/java/ovh/herisson/Clyde/Tables/Minerval.java b/backend/src/main/java/ovh/herisson/Clyde/Tables/Minerval.java
new file mode 100644
index 0000000..b54e5db
--- /dev/null
+++ b/backend/src/main/java/ovh/herisson/Clyde/Tables/Minerval.java
@@ -0,0 +1,64 @@
+package ovh.herisson.Clyde.Tables;
+
+import jakarta.persistence.Entity;
+import jakarta.persistence.GeneratedValue;
+import jakarta.persistence.GenerationType;
+import jakarta.persistence.Id;
+
+@Entity
+public class Minerval {
+    @Id
+    @GeneratedValue(strategy = GenerationType.AUTO)
+    private long id;
+
+    private long studentRegNo;
+    private int paidAmount;
+    private int toPay;
+
+    //If the academic year is 2023-2024 then 2023 will be stored here (we take the lowest year)
+    private int year;
+    public Minerval(){}
+
+    public Minerval(long studentRegNo, int paidAmount, int toPay, int year){
+        this.studentRegNo = studentRegNo;
+        this.paidAmount = paidAmount;
+        this.toPay = toPay;
+        this.year = year;
+    }
+
+    public long getStudentRegNo() {
+        return studentRegNo;
+    }
+
+    public void setStudentRegNo(long studentRegNo) {
+        this.studentRegNo = studentRegNo;
+    }
+
+    public int getPaidAmount() {
+        return paidAmount;
+    }
+
+    public void setPaidAmount(int paidAmount) {
+        this.paidAmount = paidAmount;
+    }
+
+    public int getToPay() {
+        return toPay;
+    }
+
+    public void setToPay(int toPay) {
+        this.toPay = toPay;
+    }
+
+    public int getYear() {
+        return year;
+    }
+
+    public void setYear(int year) {
+        this.year = year;
+    }
+
+    public long getId() {
+        return id;
+    }
+}