Ejemplo n.º 1
0
        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
                }));
            }
        }