Exemplo n.º 1
0
        public async Task <IActionResult> Create([FromBody] CitySaveDto resourceSave)
        {
            _auditTrailRepo.User = User;
            var activity  = Validator.GenerateActivity(resourceSave, TransactionType.ADD);
            var validator = await _validator.Validate(resourceSave);

            if (validator.HasError)
            {
                await _auditTrailRepo.SaveFailedTrail(new AuditTrailFailedSaveDto()
                {
                    Action       = TransactionType.ADD,
                    Activity     = activity,
                    ErrorMessage = validator.Message
                });

                return(BadRequest(validator.Message));
            }
            var city = _mapper.Map <City>(resourceSave);

            _crudPattern.Create(city);

            if (!await _crudPattern.SaveChanges())
            {
                return(BadRequest("Nothing has been Saved!"));
            }


            await _auditTrailRepo.SaveSuccessTrail(new AuditTrailSuccessSaveDto()
            {
                Action   = TransactionType.ADD,
                Activity = activity
            });

            return(StatusCode(201));
        }
 public async Task <ErrorValidator> Validate(CitySaveDto resourceSave)
 {
     if (resourceSave.Name.ToLower().Contains("city"))
     {
         return(new ErrorValidator("Input has 'City' Keyword please remove it!"));
     }
     if (await isCityAlreadyExisted(resourceSave.Name))
     {
         return(new ErrorValidator("This City Already Existed"));
     }
     return(new ErrorValidator());
 }
Exemplo n.º 3
0
        private async Task AddAsync()
        {
            ConsoleHelper.ShowHeading("Add city");

            string name      = ConsoleHelper.GetString("Enter name: ");
            int    countryId = ConsoleHelper.GetInt("Enter country id: ");

            CitySaveDto citySaveDto = new CitySaveDto
            {
                Name      = name,
                CountryID = countryId
            };

            await ConsoleHelper.RunWithTryCatchAsync(async() =>
            {
                SaveResponseDto <CityReadDto> saveResponseDto = await _cityApi.AddAsync(citySaveDto);
                ConsoleHelper.ShowSaveResponseDto(saveResponseDto);
            }, "An error occurred while saving data.");
        }
Exemplo n.º 4
0
        public async Task <IActionResult> Update([FromRoute] int id, [FromBody] CitySaveDto resourceSave)
        {
            _auditTrailRepo.User = User;
            var city = await _crudPattern.Get(id);

            if (city == null)
            {
                return(NotFound());
            }
            var activity  = Validator.GetDifferences(city, _mapper.Map <City>(resourceSave));
            var validator = await _validator.Validate(city, resourceSave);

            if (validator.HasError)
            {
                await _auditTrailRepo.SaveFailedTrail(new AuditTrailFailedSaveDto()
                {
                    Action       = TransactionType.EDIT,
                    Activity     = activity,
                    ErrorMessage = validator.Message,
                });

                return(BadRequest(validator.Message));
            }

            _crudPattern.Update(resourceSave, city);

            if (!await _crudPattern.SaveChanges())
            {
                return(BadRequest("Nothing has been Saved!"));
            }


            await _auditTrailRepo.SaveSuccessTrail(new AuditTrailSuccessSaveDto()
            {
                Action   = TransactionType.EDIT,
                Activity = activity
            });

            return(Ok(city));
        }