class FinanceChecksNotesAddNoteController {

    @PreAuthorize("hasPermission(#projectId, 'org.innovateuk.ifs.project.resource.ProjectCompositeId', 'ACCESS_FINANCE_CHECKS_NOTES_SECTION')")
    @PostMapping(params = "uploadAttachment")
    public String saveNewNoteAttachment(Model model,
                                        @P("projectId")@PathVariable final Long projectId,
                                        @PathVariable final Long organisationId,
                                        @ModelAttribute(FORM_ATTR) FinanceChecksNotesAddNoteForm form,
                                        @SuppressWarnings("unused") BindingResult bindingResult,
                                        ValidationHandler validationHandler,
                                        UserResource loggedInUser,
                                        HttpServletRequest request,
                                        HttpServletResponse response) {
        if (postParametersMatchOrigin(request, projectId, organisationId, loggedInUser.getId())) {
            List<Long> attachments = loadAttachmentsFromCookie(request, projectId, organisationId);
            Supplier<String> onSuccess = () -> redirectTo(rootView(projectId, organisationId));
            Supplier<String> onError = () -> {
                model.addAttribute("model", populateNoteViewModel(projectId, organisationId, attachments));
                model.addAttribute("form", form);
                return NEW_NOTE_VIEW;
            };

            return validationHandler.performActionOrBindErrorsToField("attachment", onError, onSuccess, () -> {
                MultipartFile file = form.getAttachment();

                ServiceResult<AttachmentResource> result = financeCheckService.uploadFile(projectId, file.getContentType(), file.getSize(), file.getOriginalFilename(), getMultipartFileBytes(file));
                result.ifSuccessful(uploadedAttachment -> {
                    attachments.add(uploadedAttachment.id);
                    saveAttachmentsToCookie(response, attachments, projectId, organisationId);
                    saveFormToCookie(response, projectId, organisationId, form);
                });

                model.addAttribute("model", populateNoteViewModel(projectId, organisationId, attachments));
                return result;
            });
        } else {
            throw new ObjectNotFoundException();
        }
    }

}