Implements the backend logic for the minerval gestion
This commit is contained in:
		@ -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);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@ -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);
 | 
			
		||||
}
 | 
			
		||||
@ -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;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user