コード例 #1
0
        public IHttpActionResult CreateNewAd(UserCreateAdBindingModel adModel)
        {
            if (!this.ModelState.IsValid)
            {
                return(this.BadRequest(this.ModelState));
            }

            var currentUserId = User.Identity.GetUserId();
            var currentUser   = this.users.All().FirstOrDefault(u => u.Id == currentUserId);

            if (currentUser == null)
            {
                return(this.BadRequest("Please login again.."));
            }

            var AdvertisementToAdd = new Advertisement()
            {
                Title        = adModel.Title,
                Text         = adModel.Text,
                ImageDataURL = adModel.ImageDataURL,
                Date         = DateTime.Now,
                Status       = AdvertisementStatus.WaitingApproval,
                CategoryId   = adModel.CategoryId,
                TownId       = adModel.TownId,
                OwnerId      = currentUserId
            };

            this.ads.Add(AdvertisementToAdd);

            this.ads.SaveChanges();

            return(this.Ok());
        }
コード例 #2
0
        public IHttpActionResult CreateNewAd(UserCreateAdBindingModel model)
        {
            // Validate the input parameters
            if (!ModelState.IsValid)
            {
                return(this.BadRequest(this.ModelState));
            }

            if (!this.ValidateImageSize(model.ImageDataURL))
            {
                return(this.BadRequest(string.Format("The image size should be less than {0}kb!", ImageKilobytesLimit)));
            }

            // Validate that the current user exists in the database
            var currentUserId = User.Identity.GetUserId();
            var currentUser   = this.Data.Users.All().FirstOrDefault(x => x.Id == currentUserId);

            if (currentUser == null)
            {
                return(this.BadRequest("Invalid user token! Please login again!"));
            }

            var ad = new Advertisement()
            {
                Title        = model.Title,
                Text         = model.Text,
                ImageDataURL = model.ImageDataURL,
                CategoryId   = model.CategoryId,
                TownId       = model.TownId,
                Date         = DateTime.Now,
                Status       = AdvertisementStatus.WaitingApproval,
                OwnerId      = currentUserId
            };

            this.Data.Ads.Add(ad);

            this.Data.SaveChanges();

            return(this.Ok(
                       new
            {
                message = "Advertisement created successfully.",
                adId = ad.Id
            }
                       ));
        }