Example #1
0
        public async Task <ActionResult <CertificateTypeViewModel> > GetCertificateTypeAsync(Guid id)
        {
            if (id == Guid.Empty)
            {
                return(BadRequest("No valid id."));
            }
            try
            {
                TaskResult <CertificateType> result = await certificateService.GetCertificateTypeAsync(id);

                if (!result.Succeeded)
                {
                    if (result.Data == null)
                    {
                        return(NotFound());
                    }
                }
                CertificateTypeViewModel certificateTypeViewModel = CertificateTypeViewModel.CreateVm(result.Data);
                return(Ok(certificateTypeViewModel));
            }
            catch (Exception ex)
            {
                string message = GetType().Name + "Error in " + nameof(GetCertificateTypeAsync);
                logger.LogError(ex, message);
                return(UnprocessableEntity(new ErrorViewModel {
                    Type = Type.Error, Message = message
                }));
            }
        }
Example #2
0
        public async Task <ActionResult <CertificateTypeViewModel> > UpdateCertificateType(
            CertificateTypeViewModel vm)
        {
            if (vm == null)
            {
                return(BadRequest("No valid CertificateType received"));
            }
            if (string.IsNullOrEmpty(vm.Name))
            {
                return(BadRequest("Name of a CertificateType cannot be empty"));
            }
            try
            {
                CertificateType oldCertificateType = (await certificateService.GetCertificateTypeAsync(vm.Id)).Data;
                if (oldCertificateType == null)
                {
                    return(NotFound("CertificateType not found"));
                }
                if (!oldCertificateType.RowVersion.SequenceEqual(vm.RowVersion))
                {
                    return(BadRequest("Outdated entity received"));
                }

                CertificateType updatedCertificateType = CertificateTypeViewModel.CreateCertificateType(vm);
                if (updatedCertificateType == null)
                {
                    return(BadRequest("Unable to convert CertificateTypeViewModel to CertificateType"));
                }

                oldCertificateType.Name  = updatedCertificateType.Name;
                oldCertificateType.Level = updatedCertificateType.Level;
                string oid = IdentityHelper.GetOid(HttpContext.User.Identity as ClaimsIdentity);
                oldCertificateType.LastEditBy = oid;

                TaskResult <CertificateType>
                result = await certificateService.UpdateCertificateTypeAsync(oldCertificateType);

                if (!result.Succeeded)
                {
                    return(UnprocessableEntity(new ErrorViewModel {
                        Type = Type.Error, Message = result.Message
                    }));
                }
                return(Ok(CertificateTypeViewModel.CreateVm(result.Data)));
            }
            catch (Exception ex)
            {
                string message = GetType().Name + "Error in " + nameof(UpdateCertificateType);
                logger.LogError(ex, message);
                return(UnprocessableEntity(new ErrorViewModel {
                    Type = Type.Error, Message = message
                }));
            }
        }
Example #3
0
        public async Task <ActionResult <CertificateTypeViewModel> > SaveCertificateTypeAsync(CertificateTypeViewModel vm)
        {
            if (vm == null)
            {
                return(BadRequest("No valid CertificateType received"));
            }
            if (string.IsNullOrEmpty(vm.Name))
            {
                return(BadRequest("Name of a CertificateType cannot be empty"));
            }
            try
            {
                CertificateType certificateType = CertificateTypeViewModel.CreateCertificateType(vm);
                if (certificateType == null)
                {
                    return(BadRequest("Unable to convert CertificateTypeViewModel to CertificateType"));
                }

                string oid = IdentityHelper.GetOid(HttpContext.User.Identity as ClaimsIdentity);
                certificateType.LastEditBy = oid;
                TaskResult <CertificateType> result;
                if (vm.Id == Guid.Empty)
                {
                    result = await certificateService.CreateCertificateTypeAsync(certificateType);
                }
                else
                {
                    return(BadRequest("Cannot update existing CertificateType with post method"));
                }

                if (!result.Succeeded)
                {
                    return(UnprocessableEntity(new ErrorViewModel {
                        Type = Type.Error, Message = result.Message
                    }));
                }
                return(Ok(CertificateTypeViewModel.CreateVm(result.Data)));
            }
            catch (Exception ex)
            {
                string message = GetType().Name + "Error in " + nameof(SaveCertificateTypeAsync);
                logger.LogError(ex, message);
                return(UnprocessableEntity(new ErrorViewModel {
                    Type = Type.Error, Message = message
                }));
            }
        }