public void UpdateUserAdvertById(int userId, int advertId, AdvertCreateUpdateModel model)
        {
            // validation
            var results = _advertValidator.Validate(model).ToArray();

            if (results.Length > 0)
            {
                throw new ValidationApiException(results);
            }

            if (_repo.GetUser(userId) == null)
            {
                throw new NotFoundApiException("User does not exist");
            }

            if (_repo.GetUserAdvertById(userId, advertId) == null)
            {
                throw new NotFoundApiException("Advert does not exist");
            }

            var entity = _advertMapper.EntityMapper(model);

            entity.UserId = userId;
            entity.Id     = advertId;
            _repo.UpdateUserAdvertById(entity);
        }
Esempio n. 2
0
 public Advert EntityMapper(AdvertCreateUpdateModel model)
 {
     return(new Advert()
     {
         Header = model.Header,
         Description = model.Description,
         ProvinceId = model.ProvinceId,
         CityId = model.CityId,
         Price = model.Price,
         Date = DateTime.ParseExact(model.Date, "dd-MM-yyyy", CultureInfo.InvariantCulture),
         State = model.State,
         Featured = model.Featured
     });
 }
        public IEnumerable <ValidationResult> Validate(AdvertCreateUpdateModel model)
        {
            var headerResult = ValidateHeader(model.Header);

            if (headerResult != null)
            {
                yield return(headerResult);
            }

            var descriptionResult = ValidateDescription(model.Description);

            if (descriptionResult != null)
            {
                yield return(descriptionResult);
            }

            var priceResult = ValidatePrice(model.Price);

            if (priceResult != null)
            {
                yield return(priceResult);
            }
        }
 public IActionResult UpdateUserAdvertById(int userId, int advertId, AdvertCreateUpdateModel model)
 {
     _userService.UpdateUserAdvertById(userId, advertId, model);
     return(NoContent());
 }
        public ActionResult <AdvertViewModel> CreateUserAdvertById(int userId, AdvertCreateUpdateModel model)
        {
            var advert = _userService.CreateUserAdvertById(userId, model);

            return(CreatedAtAction(nameof(GetUserAdvertById), new { userId = userId, advertId = advert.Id }, advert));
        }