<repo_name>Tiance-Zhang/clinic-backend<file_sep>/src/main/java/com/harilab/clinic/service/AuthService.java
package com.harilab.clinic.service;

import com.harilab.clinic.model.Clinician;
import com.harilab.clinic.repository.ClinicianRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class AuthService {

    @Autowired
    private ClinicianRepository repository;

    public boolean clinicianLogin(Clinician clinician) {
        Clinician other = repository.findClinicianByNameAndPassword(clinician.getUsername(), clinician.getPassword());
        return other != null;
    }
}
<file_sep>/src/main/java/com/harilab/clinic/service/ClinicianService.java
package com.harilab.clinic.service;

import com.harilab.clinic.model.Clinician;
import com.harilab.clinic.repository.ClinicianRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ClinicianService {
    @Autowired
    private ClinicianRepository repository;

    // create clinician
    public Clinician saveClinician(Clinician clinician){
        return repository.save(clinician);
    }
    public List<Clinician> saveClinicians(List<Clinician> clinicians){
        return repository.saveAll(clinicians);
    }

    // get clinician
    public List<Clinician> getClinicians(){
        return repository.findAll();
    }
    public Clinician getClinicianById(int id){
        return repository.findById(id).orElse(null);
    }
//    public Clinician getClinicianByName(String name){
//        return repository.findByName(name);
//    }

    // delete clinician
    public String deleteClinicianById(int id){
        repository.deleteById(id);
        return "Clinician removed !!" + id;
    }

    // update clinician
    public Clinician updateClinician(Clinician clinician){
        Clinician existingClinician = repository.findById(clinician.getId()).orElse(null);
        existingClinician.setUsername(clinician.getUsername());
        existingClinician.setPassword(<PASSWORD>ician.<PASSWORD>());
        return repository.save(existingClinician);
    }

}
<file_sep>/src/main/java/com/harilab/clinic/controller/ParticipantController.java
package com.harilab.clinic.controller;

import com.harilab.clinic.model.Participant;
import com.harilab.clinic.service.ParticipantService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class ParticipantController {

    @Autowired
    private ParticipantService service;

    @CrossOrigin
    @PostMapping("/participants")
    public Participant addParticipant(@RequestBody Participant participant){
        return service.saveParticipant(participant);
    }


    @GetMapping("/participants")
    @CrossOrigin
    public List<Participant> getParticipants(){
        return service.getParticipants();
    }

    @CrossOrigin
    @GetMapping("/participants/alert")
    public List<Participant> getParticipantsInAlertOrder(){
        return service.getParticipantInAlertOrder();
    }

    @CrossOrigin
    @GetMapping("/participants/{id}")
    public Participant getParticipantById(@PathVariable int id){
        return service.getParticipantById(id);
    }

//    @CrossOrigin
//    @GetMapping("/participants/{name}")
//    public Participant getParticipantByName(@PathVariable String name){
//        return service.getParticipantByName(name);
//    }

//    @CrossOrigin
//    @GetMapping("/participants/{device}")
//    public Participant getParticipantByDevice(@PathVariable String device){
//        return service.getParticipantByDevice(device);
//    }

    @CrossOrigin
    @PatchMapping("/participants")
    public Participant updateParticipant(@RequestBody Participant participant){
        return service.updateParticipant(participant);
    }

    @CrossOrigin
    @DeleteMapping("/participants/{id}")
    public String deleteParticipant(@PathVariable int id){
        return service.deleteParticipantById(id);
    }


}
<file_sep>/src/main/java/com/harilab/clinic/controller/AuthController.java
package com.harilab.clinic.controller;

import com.harilab.clinic.model.Clinician;
import com.harilab.clinic.service.AuthService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class AuthController {

    @Autowired
    private AuthService service;

    @CrossOrigin
    @PostMapping("/login")
    public boolean clinicianLogin(@RequestBody Clinician clinician){
        return service.clinicianLogin(clinician);
    }
}
<file_sep>/src/main/java/com/harilab/clinic/controller/RecordController.java
package com.harilab.clinic.controller;

import com.harilab.clinic.model.Record;
import com.harilab.clinic.service.RecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;


@RestController
public class RecordController {

    @Autowired
    private RecordService service;

    @PostMapping("/participants/{pid}/records")
    @CrossOrigin
    public Record addRecord(@PathVariable int pid, @RequestBody Record record) {
        return service.saveRecord(pid, record);
    }

    @CrossOrigin
    @GetMapping("/records/{id}")
    public Record getRecordById(@PathVariable int id){
        return service.getRecordById(id);
    }

    @CrossOrigin
    @GetMapping("/participants/{pid}/records")
    public List<Record> getRecordsByParticipant(@PathVariable int pid){
        return service.getRecordsByParticipant(pid);
    }

    @CrossOrigin
    @GetMapping("/participants/{pid}/records/latest")
    public Record getLatestRecordByParticipant(@PathVariable int pid){
        return service.getLatestRecordByParticipant(pid);
    }

    @CrossOrigin
    @GetMapping("/participants/{pid}/usage/lastweek")
    public int[] getWeeklyUsageByParticipant(@PathVariable int pid){
        return service.getWeeklyUsageByParticipant(pid);
    }

    @CrossOrigin
    @GetMapping("/participants/{pid}/records/today")
    public List<Record> getTodayRecordsByParticipant(@PathVariable int pid){
        return service.getTodayRecordsByParticipant(pid);
    }

    @CrossOrigin
    @PatchMapping("/records")
    public Record updateRecord(@RequestBody Record record) {
        return service.updateRecord(record);
    }

    @CrossOrigin
    @DeleteMapping("/records/{id}")
    public String deleteRecord(@PathVariable int id) {
        return service.deleteRecordById(id);
    }

}
<file_sep>/src/main/java/com/harilab/clinic/service/ParticipantService.java
package com.harilab.clinic.service;

import com.harilab.clinic.model.Participant;
import com.harilab.clinic.repository.ParticipantRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.Instant;
import java.time.LocalDate;
import java.time.Period;
import java.time.ZoneId;
import java.util.Comparator;
import java.util.List;

@Service
public class ParticipantService {
    @Autowired
    private ParticipantRepository repository;

    // create participant
    public Participant saveParticipant(Participant participant) {
        return repository.save(participant);
    }

    public List<Participant> saveParticipants(List<Participant> participants) {
        return repository.saveAll(participants);
    }

    // get participant
    public List<Participant> getParticipants() {
        return repository.findAll();
    }

    public List<Participant> getParticipantInAlertOrder(){
        List<Participant> result = getParticipants();
        // update
        for(Participant p: result){
            participantAlertUpdateHelper(p);
        }
        result.sort(new CompareByAlertDays());
        return result;
    }

    private void participantAlertUpdateHelper(Participant participant) {
        LocalDate currentDate = LocalDate.now();
        LocalDate lastRecordDate = Instant.ofEpochMilli(participant.getLast_record_time().getTime()).atZone(ZoneId.systemDefault()).toLocalDate();
        int days = Period.between(lastRecordDate, currentDate).getDays();
        participant.setAlert_days(days);
        repository.save(participant);
    }

    public Participant getParticipantById(int id) {
        return repository.findById(id).orElse(null);
    }
//    public Participant getParticipantByName(String name){
//        return repository.findByName(name);
//    }
//    public Participant getParticipantByDevice(String device){
//        return repository.findByDevice(device);
//    }
//    public Participant getParticipantsByClinicianId(int cid){ return repository.findByClinicianId(cid); }

    // delete participant
    public String deleteParticipantById(int id) {
        repository.deleteById(id);
        return "Participant removed !!" + id;
    }

    // update participant
    public Participant updateParticipant(Participant participant) {
        Participant existingParticipant = repository.findById(participant.getId()).orElse(null);
        existingParticipant.setUsername(participant.getUsername());
        existingParticipant.setDevice_id(participant.getDevice_id());
        return repository.save(existingParticipant);
    }

    /**
     * Comparator class used to sort record in reversed time order.
     */
    private class CompareByAlertDays implements Comparator<Participant> {
        public int compare(Participant one, Participant two) {
            return Integer.compare (two.getAlert_days(), one.getAlert_days());
        }
    }
}
