// Validate
        public IResponseDTO ValidateIdentificationType(CreateUpdateIdentificationTypeDto options, int id = 0)
        {
            try
            {
                if (_appDbContext.IdentificationTypes.Any(x => x.Id != id && !x.IsDeleted && x.Name.ToLower().Trim() == options.Name.ToLower().Trim()))
                {
                    _response.Errors.Add($"Name '{options.Name}' is already exist, please try a new one.'");
                }
            }
            catch (Exception ex)
            {
                _response.IsPassed = false;
                _response.Data     = null;
                _response.Errors.Add($"Error: {ex.Message}");
            }

            if (_response.Errors.Count > 0)
            {
                _response.IsPassed = false;
                _response.Data     = null;
                return(_response);
            }

            _response.IsPassed = true;
            return(_response);
        }
        public async Task <IResponseDTO> CreateIdentificationType(CreateUpdateIdentificationTypeDto options, int userId)
        {
            try
            {
                var identificationType = new Data.DbModels.LookupSchema.IdentificationType
                {
                    Name        = options.Name,
                    Description = options.Description,
                    IsActive    = true
                };

                await _appDbContext.IdentificationTypes.AddAsync(identificationType);

                // save to the database
                var save = await _appDbContext.SaveChangesAsync();

                if (save == 0)
                {
                    _response.IsPassed = false;
                    _response.Errors.Add("Database did not save the object");
                    return(_response);
                }

                _response.IsPassed = true;
                _response.Message  = "Identification type is created successfully";
            }
            catch (Exception ex)
            {
                _response.Data     = null;
                _response.IsPassed = false;
                _response.Errors.Add($"Error: {ex.Message}");
            }

            return(_response);
        }
Пример #3
0
        public async Task <IResponseDTO> CreateIdentificationType([FromBody] CreateUpdateIdentificationTypeDto options)
        {
            var validationResult = _identificationTypeService.ValidateIdentificationType(options);

            if (!validationResult.IsPassed)
            {
                return(validationResult);
            }

            _response = await _identificationTypeService.CreateIdentificationType(options, LoggedInUserId);

            return(_response);
        }
        public async Task <IResponseDTO> UpdateIdentificationType(int id, CreateUpdateIdentificationTypeDto options, int userId)
        {
            try
            {
                var identificationType = _appDbContext.IdentificationTypes.FirstOrDefault(x => x.Id == id);
                if (identificationType == null)
                {
                    _response.IsPassed = false;
                    _response.Message  = "Invalid id";
                    return(_response);
                }

                identificationType.Name        = options.Name;
                identificationType.Description = options.Description;

                _appDbContext.IdentificationTypes.Update(identificationType);

                // save to the database
                var save = await _appDbContext.SaveChangesAsync();

                if (save == 0)
                {
                    _response.IsPassed = false;
                    _response.Errors.Add("Database did not save the object");
                    return(_response);
                }

                _response.IsPassed = true;
                _response.Message  = "Identification type is updated successfully";
            }
            catch (Exception ex)
            {
                _response.Data     = null;
                _response.IsPassed = false;
                _response.Errors.Add($"Error: {ex.Message}");
            }

            return(_response);
        }