public async Task <ActionResult <CertificateViewModel> > SaveCertificateAsync(CertificateViewModel vm) { if (vm == null) { return(BadRequest("No valid Certificate received")); } if (vm.Person == null || vm.CertificateType == null) { return(BadRequest("No valid Certificate received")); } try { Certificate certificate = CertificateViewModel.CreateCertificate(vm); if (certificate == null) { return(BadRequest("Unable to convert CertificateViewModel to Certificate")); } string oid = IdentityHelper.GetOid(HttpContext.User.Identity as ClaimsIdentity); certificate.LastEditBy = oid; certificate.CertificateType = null; certificate.Person = null; TaskResult <Certificate> result; if (vm.Id == Guid.Empty) { result = await certificateService.CreateCertificateAsync(certificate); } else { return(BadRequest("Cannot update existing Certificate with post method")); } if (!result.Succeeded) { return(UnprocessableEntity(new ErrorViewModel { Type = Type.Error, Message = result.Message })); } return(Ok(CertificateViewModel.CreateVm(result.Data))); } catch (Exception ex) { string message = GetType().Name + "Error in " + nameof(SaveCertificateAsync); logger.LogError(ex, message); return(UnprocessableEntity(new ErrorViewModel { Type = Type.Error, Message = message })); } }
public async Task <ActionResult <CertificateViewModel> > UpdateCertificateAsync(CertificateViewModel vm) { if (vm == null) { return(BadRequest("No valid Certificate received")); } if (vm.Person == null || vm.CertificateType == null || vm.Id == Guid.Empty) { return(BadRequest("No valid Certificate received")); } try { Certificate oldCertificate = (await certificateService.GetCertificateAsync(vm.Id)).Data; if (oldCertificate == null) { return(NotFound("Certificate not found")); } if (!oldCertificate.RowVersion.SequenceEqual(vm.RowVersion)) { return(BadRequest("Outdated entity received")); } if (oldCertificate.PersonId != vm.Person.Id) { return(BadRequest("Cannot transfer ownership of a certificate")); } Certificate updatedCertificate = CertificateViewModel.CreateCertificate(vm); if (updatedCertificate == null) { return(BadRequest("Unable to convert CertificateTypeViewModel to CertificateType")); } oldCertificate.DateIssued = updatedCertificate.DateIssued; oldCertificate.DateExpired = updatedCertificate.DateExpired; oldCertificate.CertificateTypeId = updatedCertificate.CertificateTypeId; oldCertificate.CertificateType = null; oldCertificate.Person = null; string oid = IdentityHelper.GetOid(HttpContext.User.Identity as ClaimsIdentity); oldCertificate.LastEditBy = oid; TaskResult <Certificate> result = await certificateService.UpdateCertificateAsync(oldCertificate); if (!result.Succeeded) { return(UnprocessableEntity(new ErrorViewModel { Type = Type.Error, Message = result.Message })); } return(Ok(CertificateViewModel.CreateVm(result.Data))); } catch (Exception ex) { string message = GetType().Name + "Error in " + nameof(UpdateCertificateAsync); logger.LogError(ex, message); return(UnprocessableEntity(new ErrorViewModel { Type = Type.Error, Message = message })); } }