<commit_before>package ee.scanner.tablet.controller;

import ee.scanner.tablet.dto.DeviceDTO;
import ee.scanner.tablet.dto.UserDTO;
import ee.scanner.tablet.dto.UserWrapperDTO;
import ee.scanner.tablet.exception.DeviceDuplicateException;
import ee.scanner.tablet.exception.IdNotPresentException;
import ee.scanner.tablet.exception.PinDuplicateException;
import ee.scanner.tablet.feedback.FeedbackType;
import ee.scanner.tablet.service.DataSaveService;
import ee.scanner.tablet.util.ControllerUtil;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;

import javax.validation.Valid;

@Controller
@AllArgsConstructor(onConstructor = @__(@Autowired))
@PreAuthorize("hasRole('ADMIN')")
public class DataController {
    private final DataSaveService dataSaveService;

    @GetMapping("/insert")
    public String insertPage(@ModelAttribute("userDTO") UserDTO userDTO,
                             @ModelAttribute("deviceDTO") DeviceDTO deviceDTO) {
        return "insert";
    }

    @PostMapping("/insert_device")
    public String insertDevice(@ModelAttribute("userDTO") UserDTO userDTO,
                               @Valid @ModelAttribute("deviceDTO") DeviceDTO deviceDTO,
                               BindingResult bindingResult,
                               Model model) {
        if (bindingResult.hasErrors()) {
            ControllerUtil.setFeedback(model, FeedbackType.ERROR, "Seadme identifikaator ei ole korrektne!");
        } else {
            try {
                dataSaveService.saveDevice(deviceDTO);
                ControllerUtil.setFeedback(model, FeedbackType.SUCCESS, "Seade salvestatud!");
            } catch (DeviceDuplicateException e) {
                ControllerUtil.setFeedback(model, FeedbackType.ERROR, "Seade juba eksisteerib!");
            }
        }
        return "insert";
    }

    @PostMapping("/insert_user")
    public String insertUser(@ModelAttribute("deviceDTO") DeviceDTO deviceDTO,
                             @Valid @ModelAttribute("userDTO") UserDTO userDTO, BindingResult bindingResult,
                             Model model) {
        if (bindingResult.hasErrors()) {
            ControllerUtil.setFeedback(model, FeedbackType.ERROR, "Kasutajaga seotud andmed ei ole korreksed!");
        } else {
            try {
                dataSaveService.saveUser(userDTO);
                ControllerUtil.setFeedback(model, FeedbackType.SUCCESS, "Uus kasutaja salvestatud!");
            } catch (PinDuplicateException e) {
                ControllerUtil.setFeedback(model, FeedbackType.ERROR, "Antud PIN on juba kasutusel! Vali uus PIN!");
            }
        }
        return "insert";
    }

    @PostMapping("/update_user")
    public String updateUser(@Valid @ModelAttribute("users") UserWrapperDTO dto,
                             BindingResult bindingResult,
                             Model model) {
        if (bindingResult.hasErrors()) {
            ControllerUtil.setFeedback(model, FeedbackType.ERROR, "Sisestatud andmed ei ole korreksed!");
        } else {
            try {
                dataSaveService.updateUsers(dto);
                ControllerUtil.setFeedback(model, FeedbackType.SUCCESS, "Kasutaja uuendatud!");
            } catch (IdNotPresentException e) {
                ControllerUtil.setFeedback(model, FeedbackType.SUCCESS, "Kasutajat ei leitud andmebaasist!");
            }
        }

        model.addAttribute("devices", dataSaveService.getAllDevices());
        return "users";
    }
}<commit_msg>Bug dix<commit_after>package ee.scanner.tablet.controller;

import ee.scanner.tablet.dto.DeviceDTO;
import ee.scanner.tablet.dto.UserDTO;
import ee.scanner.tablet.dto.UserWrapperDTO;
import ee.scanner.tablet.exception.DeviceDuplicateException;
import ee.scanner.tablet.exception.IdNotPresentException;
import ee.scanner.tablet.exception.PinDuplicateException;
import ee.scanner.tablet.feedback.FeedbackType;
import ee.scanner.tablet.service.DataSaveService;
import ee.scanner.tablet.util.ControllerUtil;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;

import javax.validation.Valid;

@Controller
@AllArgsConstructor(onConstructor = @__(@Autowired))
@PreAuthorize("hasRole('ADMIN')")
public class DataController {
    private final DataSaveService dataSaveService;

    @GetMapping("/insert")
    public String insertPage(@ModelAttribute("userDTO") UserDTO userDTO,
                             @ModelAttribute("deviceDTO") DeviceDTO deviceDTO) {
        return "insert";
    }

    @PostMapping("/insert_device")
    public String insertDevice(@ModelAttribute("userDTO") UserDTO userDTO,
                               @Valid @ModelAttribute("deviceDTO") DeviceDTO deviceDTO,
                               BindingResult bindingResult,
                               Model model) {
        if (bindingResult.hasErrors()) {
            ControllerUtil.setFeedback(model, FeedbackType.ERROR, "Seadme identifikaator ei ole korrektne!");
        } else {
            try {
                dataSaveService.saveDevice(deviceDTO);
                ControllerUtil.setFeedback(model, FeedbackType.SUCCESS, "Seade salvestatud!");
            } catch (DeviceDuplicateException e) {
                ControllerUtil.setFeedback(model, FeedbackType.ERROR, "Seade juba eksisteerib!");
            }
        }
        return "insert";
    }

    @PostMapping("/insert_user")
    public String insertUser(@ModelAttribute("deviceDTO") DeviceDTO deviceDTO,
                             @Valid @ModelAttribute("userDTO") UserDTO userDTO, BindingResult bindingResult,
                             Model model) {
        if (bindingResult.hasErrors()) {
            ControllerUtil.setFeedback(model, FeedbackType.ERROR, "Kasutajaga seotud andmed ei ole korreksed!");
        } else {
            try {
                dataSaveService.saveUser(userDTO);
                ControllerUtil.setFeedback(model, FeedbackType.SUCCESS, "Uus kasutaja salvestatud!");
            } catch (PinDuplicateException e) {
                ControllerUtil.setFeedback(model, FeedbackType.ERROR, "Antud PIN on juba kasutusel! Vali uus PIN!");
            }
        }
        return "insert";
    }

    @PostMapping("/update_user")
    public String updateUser(@Valid @ModelAttribute("users") UserWrapperDTO dto,
                             BindingResult bindingResult,
                             Model model) {
        if (bindingResult.hasErrors()) {
            ControllerUtil.setFeedback(model, FeedbackType.ERROR, "Sisestatud andmed ei ole korreksed!");
        } else {
            try {
                dataSaveService.updateUsers(dto);
                ControllerUtil.setFeedback(model, FeedbackType.SUCCESS, "Kasutaja uuendatud!");
            } catch (IdNotPresentException e) {
                ControllerUtil.setFeedback(model, FeedbackType.SUCCESS, "Kasutajat ei leitud andmebaasist!");
            }
        }

        model.addAttribute("users", dataSaveService.getAllUsers());
        return "users";
    }
}
{'fraction_non_alphanumeric': 0.09619800969635112, 'fraction_numerical': 0.0, 'mean_word_length': 2.812743190661479, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '256474', 'n_tokens_mistral': 2155, 'n_tokens_neox': 2132, 'n_words': 379}