Exemplo n.º 1
0
        public async Task <ActionResult <DocumentViewModel> > CreateDocument(DocumentViewModel documentViewModel)
        {
            if (documentViewModel?.Name == null || documentViewModel.DocumentUri == null)
            {
                return(BadRequest("No valid document received"));
            }
            try
            {
                string oid = IdentityHelper.GetOid(HttpContext.User.Identity as ClaimsIdentity);

                if (documentViewModel.Name == "Privacy Policy" && !PersonsController.UserHasRole(UserRole.Boardmember,
                                                                                                 (ClaimsIdentity)HttpContext.User.Identity))
                {
                    return(Unauthorized());
                }

                Document document = DocumentViewModel.CreateDocument(documentViewModel);
                if (document == null)
                {
                    return(BadRequest("Unable to convert DocumentViewModel to Document"));
                }

                document.LastEditBy = oid;
                TaskResult <Document> result;
                if (document.Id == Guid.Empty)
                {
                    result = await documentService.CreateDocumentAsync(document);
                }
                else
                {
                    return(BadRequest("Cannot update existing document with post method"));
                }
                if (!result.Succeeded)
                {
                    return(UnprocessableEntity(new ErrorViewModel {
                        Type = Type.Error, Message = result.Message
                    }));
                }
                return(Ok(DocumentViewModel.CreateVm(result.Data)));
            }
            catch (Exception ex)
            {
                string message = GetType().Name + "Error in " + nameof(CreateDocument);
                logger.LogError(ex, message);
                return(UnprocessableEntity(new ErrorViewModel {
                    Type = Type.Error, Message = message
                }));
            }
        }
Exemplo n.º 2
0
        public async Task <ActionResult <DocumentViewModel> > DeleteDocumentAsync(Guid id)
        {
            if (id == Guid.Empty)
            {
                return(BadRequest("No valid id."));
            }
            try
            {
                Document document = (await documentService.GetDocumentAsync(id)).Data;
                if (document == null)
                {
                    return(NotFound("Document not found"));
                }

                if (document.Name != "profilepicture" && !PersonsController.UserHasRole(UserRole.Boardmember,
                                                                                        (ClaimsIdentity)HttpContext.User.Identity))
                {
                    return(Unauthorized("User is cannot delete this file"));
                }

                TaskResult <Document> removeDocumentResult = await documentService.DeleteDocumentAsync(document);

                return(!removeDocumentResult.Succeeded
                    ? UnprocessableEntity(new ErrorViewModel
                {
                    Type = Type.Error, Message = removeDocumentResult.Message
                })
                    : Ok(DocumentViewModel.CreateVm(removeDocumentResult.Data)));
            }
            catch (Exception ex)
            {
                logger.LogError(ex, GetType().Name + "Error in " + nameof(DeleteAsync));
                return(UnprocessableEntity(new UploadResultViewModel {
                    Succeeded = false
                }));
            }
        }