Example #1
0
        public IActionResult PostAd(AdFullDTO newAd)
        {
            try
            {
                string message = "";
                int    newAdID = _unitOfWork.SaveNewAd(newAd, out message);

                var res = new
                {
                    AdID    = newAdID,
                    Message = message
                };

                return(new JsonResult(res));
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, $"Error invoking PostAd - {ex.Message}");
                return(new StatusCodeResult(500));
            }
        }
Example #2
0
        public int SaveNewAd(AdFullDTO ad, out string message)
        {
            message = "";
            if (ad.Title.Length > 200)
            {
                message = "Title is more then 200 characters";
                return(-1);
            }
            if (ad.Description.Length > 1000)
            {
                message = "Description is more then 1000 characters";
                return(-1);
            }
            if (ad.PhotoLinks.Count() > 3)
            {
                message = "More than 3 photo links is not allowed";
                return(-1);
            }

            Ad newAd = new Ad();

            newAd.Title       = ad.Title;
            newAd.Description = ad.Description;
            newAd.Price       = ad.Price;
            if (ad.PhotoLinks.Count() > 0)
            {
                var newPhotoLinks = new List <Photo>();
                foreach (var photoLink in ad.PhotoLinks)
                {
                    Photo newPhoto = new Photo();
                    newPhoto.PhotoURL = photoLink;
                    newPhotoLinks.Add(newPhoto);
                }
                newAd.Photos = newPhotoLinks;
            }
            Ads.Add(newAd);
            return(newAd.Id);
        }