John-sCC / jcc_backend

Eat up!
1 stars 0 forks source link

Backend: StappletData API #37

Open Toby-Leeder opened 5 months ago

Toby-Leeder commented 5 months ago
public void addQrCodeToUser(String personEmail, Long qrCodeId) { 
    Person person = personJpaRepository.findByEmail(personEmail);
        if (person != null) {   // verify person
            Optional<QrCode> qrCode = QrCodeJpaRepository.findById(qrCodeId);
            if (qrCode != null) { // verify role
                person.getQrCodes().add(qrCode.get());
            }
        }
    }
@GetMapping("/getQuantitative{id}")
public ResponseEntity<Quantitative> getQuantitative(@PathVariable long id) {
        Optional<Quantitative> optional = qRepository.findById(id);
        if (optional.isPresent()) {  // Good ID
            Quantitative quantitative = optional.get();  // value from findByID
            return new ResponseEntity<>(quantitative, HttpStatus.OK);  // OK HTTP response: status code, headers, and body
        }
        // Bad ID
        return new ResponseEntity<>(HttpStatus.BAD_REQUEST);       
    }
function postApi(){
    if(window.location.href.includes("127.0.0.1")){
        var url = 'http://localhost:8911/api/stats/newQuantitative';
    }
    else {
        var url = 'https://https://jcc.stu.nighthawkcodingsociety.com/api/stats/newQuantitative'; 
    }

    var name = document.getElementById('variableName').value;
    var variable = document.getElementById('variable').value.split(',');

    const quantitativeRequest = {
        data: variable, 
        name: name
    };

    fetch(url, {
        method: 'POST',
        credentials: 'include',
        headers: {
            'Content-Type': 'application/json',
        },
        body: JSON.stringify(quantitativeRequest)
    })
    .then(response => response.json())
    .then(data => console.log('Success:', data))
    .catch(error => {
        console.error('Error:', error);
        if (error instanceof SyntaxError) {
            console.error('There was a syntax error in the response, possibly not JSON:', error.message);
        } else {
            console.error('There was a network or other error:', error.message);
        }
    });
}
    @PostMapping("/newTwoQuantitative")
    public ResponseEntity<TwoQuantitative> newCode(@CookieValue("jwt") String jwtToken, @RequestBody TwoQuantitativeRequest twoQuantitativeRequest) {
        List<Double> explanatory = twoQuantitativeRequest.getExplanatory();
        List<Double> response = twoQuantitativeRequest.getResponse();
        String explanatoryName = twoQuantitativeRequest.getExplanatoryName(); 
        String responseName = twoQuantitativeRequest.getResponseName(); 

        Quantitative quantitative1 = new Quantitative(explanatory, explanatoryName);
        Quantitative quantitative2 = new Quantitative(response, responseName);

        qRepository.save(quantitative1);
        qRepository.save(quantitative2);

        String userEmail = tokenUtil.getUsernameFromToken(jwtToken);

        List<List<Double>> inputs = new ArrayList<>();

        inputs.add(explanatory);
        inputs.add(response);

        TwoQuantitative twoQuantitative = new TwoQuantitative(inputs, quantitative1.getId(), quantitative2.getId());

        twoQRepository.save(twoQuantitative);

        twoQuantitativeDetailsService.addTwoQuantitativeToUser(userEmail, twoQuantitative.getId());

        return new ResponseEntity<>(twoQuantitative, HttpStatus.OK);
    }    

Image

    public List<Double> customSort(List<Double> arr){
        int n = arr.size();
        for (int i = 1; i < n; ++i) {
            double key = arr.get(i);
            int j = i - 1;

            while (j >= 0 && arr.get(j) > key) {
                arr.set(j + 1, arr.get(j));
                j = j - 1;
            }
            arr.set(j + 1, key);
        }
        return arr;
    }
    public void addTwoQuantitativeToUser(String personEmail, Long twoQuantitativeId) { 
        Person person = personJpaRepository.findByEmail(personEmail);
        if (person != null) {   // verify person
            Optional<TwoQuantitative> twoQuantitative = twoQuantitativeJpaRepository.findById(twoQuantitativeId);
            if (twoQuantitative != null) { // verify role
                person.getTwoQuantitatives().add(twoQuantitative.get());
            }
        }
    }