Example #1
0
        public IHttpActionResult CreateAd([FromUri]CreateAdBindingModel model)
        {
            var ad = new Ad
            { 
                Name = model.Name,
                Description = model.Description,
                TypeId = model.TypeId,
                Price = model.Price,
                PostedOn = DateTime.Now,
                OwnerId = this.User.Identity.GetUserId()
            };

            foreach (var categoryId in model.Categories)
            {
                var category = this.Data.Categories.Find(categoryId);
                ad.Categories.Add(category);
            }

            this.Data.Ads.Add(ad);
            this.Data.SaveChanges();

            var newAd = this.Data.Ads
                .Where(a => a.Id == ad.Id)
                .Select(AdViewModel.Create)
                .FirstOrDefault();
                
            return this.Ok(newAd);
        }
        public IHttpActionResult PutCreateAd(CreateAdBindingModel model)
        {
            if (model == null)
            {
                return this.BadRequest("Model cannot be empty!");
            }

            if (!this.ModelState.IsValid)
            {
                return this.BadRequest(this.ModelState);
            }

            if (!model.Categories.Any() || model.Categories.Count() > 3)
            {
                return this.BadRequest("Categories count cannot be " + model.Categories.Count());
            }

            var dbType = this.Data.AdTypes.All().FirstOrDefault(c => c.Id == model.TypeId);
            if (dbType == null)
            {
                return this.BadRequest("No type with id " + model.TypeId + " found!");
            }

            var categories = new List<Category>();
            foreach (var categoryId in model.Categories)
            {
                var dbCategory = this.Data.Categories.All().FirstOrDefault(c => c.Id == categoryId);
                if (dbCategory == null)
                {
                    return this.BadRequest("No category with id " + model.TypeId + " found!");
                }

                categories.Add(dbCategory);
            }

            var userId = this.User.Identity.GetUserId();
            var user = this.Data.Users.All().FirstOrDefault(u => u.Id == userId);
            var newAd = new Ad()
            {
                Categories = categories,
                PostedOn = DateTime.Now,
                Description = model.Description,
                Name = model.Name,
                Owner = user,
                Price = model.Price,
                Type = dbType
            };

            this.Data.Ads.Add(newAd);
            this.Data.SaveChanges();

            var dbAd = this.Data.Ads.All()
                .Where(a => a.Id == newAd.Id)
                .Select(AdViewModel.Create)
                .FirstOrDefault();

            return this.Ok(dbAd);
        }
        public IHttpActionResult CreateNewAd(CreateAdBindingModel model)
        {
            if (model == null)
            {
                return this.BadRequest("model is empty");
            }

            if (!this.ModelState.IsValid)
            {
                return this.BadRequest(this.ModelState);
            }

            if (!this.Data.AdTypes.All().Any(t => t.Id == model.TypeId))
            {
                string badRequest = string.Format("There is no type with id - {0}",
                    model.TypeId);
                return this.BadRequest(badRequest);
            }

            var userId = this.User.Identity.GetUserId();
            var ad = new Ad
            {
                Name = model.Name,
                Description = model.Description,
                Price = model.Price,
                PostedOn = DateTime.Now,
                Status = AdStatus.Open,
                OwnerId = userId,
                TypeId = model.TypeId
            };

            if(model.Categories.Count() >= 1 && model.Categories.Count() <= 3){
                foreach (var categoryId in model.Categories)
                {
                    var category = this.Data.Categories.Find(categoryId);
                    if (category == null)
                    {
                        return this.BadRequest("Non existing category");
                    }

                    ad.Categories.Add(category);
                }
            }
            else
            {
                return this.BadRequest("You can add at least 1 category and no more than 3");
            }

            this.Data.Ads.Add(ad);
            this.Data.SaveChanges();

            var result = this.Data.Ads.All()
                .Where(a => a.Id == ad.Id)
                .Select(AdViewModel.Create)
                .FirstOrDefault();

            return this.Ok(result);
        }
Example #4
0
        public IHttpActionResult CreateNewAd(CreateAdBindingModel model)
        {
            var userId = this.User.Identity.GetUserId();
            if (userId == null)
            {
                return this.Unauthorized();
            }

            if (model == null)
            {
                return this.BadRequest("model is empty");
            }

            if (!this.ModelState.IsValid)
            {
                return this.BadRequest(this.ModelState);
            }

            if (!this.Data.AdTypes.Any(t => t.Id == model.TypeId))
            {
                return this.BadRequest("there is no type with the provided id in the database");
            }

            var ad = new Ad
            {
                Name = model.Name,
                Description = model.Description,
                Price = model.Price,
                PostedOn = DateTime.Now,
                Status = AdStatus.Open,
                OwnerId = userId,
                TypeId = model.TypeId
            };

            foreach (var categoryId in model.Categories)
            {
                var category = this.Data.Categories.Find(categoryId);
                if (category == null)
                {
                    return this.BadRequest("Non existing category");
                }

                ad.Categories.Add(category);
            }

            this.Data.Ads.Add(ad);
            this.Data.SaveChanges();
            var result = this.Data.Ads
                .Where(a => a.Id == ad.Id)
                .Select(AdViewModel.Create)
                .FirstOrDefault();

            return this.Ok(result);
        }
        public IHttpActionResult CreateAd(CreateAdBindingModel model)
        {
            if (!this.ModelState.IsValid)
            {
                return BadRequest(this.ModelState);
            }

            if (model == null)
            {
                return BadRequest("Missing data!");
            }

            if (model.Categories.Count() < 0 && model.Categories.Count() > 3)
            {
                return BadRequest("Invalid data!1");
            }

            if (model.Categories.Any(category => !this.Data.Categories.Any(c => c.Id == category)))
            {
                return BadRequest("Invalid data!2");
            }

            if (!this.Data.AdTypes.Any(t => t.Id == model.TypeId))
            {
                return BadRequest("Invalid data!3");
            }

            string userId = User.Identity.GetUserId();
            var user = this.Data.Users.FirstOrDefault(x => x.Id == userId);
            var ad = new Ad
            {
                Name = model.Name,
                Price = model.Price,
                Description = model.Description,
                TypeId = model.TypeId,
                PostedOn = DateTime.Now,
                Owner = user

            };

            foreach (var c in model.Categories)
            {
                ad.Categories.Add(Data.Categories.FirstOrDefault(x => x.Id == c));
            }

            this.Data.Ads.AddOrUpdate(ad);
            this.Data.SaveChanges();

            AdViewModel result = this.Data.Ads
                .Where(a => a.Id == ad.Id)
                .Select(AdViewModel.Create)
                .FirstOrDefault();
            return Ok(result);
        }
        public IHttpActionResult CreateAd(AdBindingModel adBindingModel)
        {
            if (adBindingModel == null)
            {
                return this.BadRequest("Your data is missing");
            }

            if (!this.ModelState.IsValid)
            {
                return this.BadRequest(this.ModelState);
            }

            if (!adBindingModel.Categories.Any() || adBindingModel.Categories.Count() > 3)
            {
                return this.BadRequest("Categories count must be between 1 and 3");
            }

            var ad = new Ad();
            ad.OwnerId = this.User.Identity.GetUserId();
            ad.Name = adBindingModel.Name;
            ad.Description = adBindingModel.Description;
            ad.Price = adBindingModel.Price;
            ad.PostedOn = DateTime.Now;

            var type = this.Data.AdTypes.FirstOrDefault(t => t.Id == adBindingModel.TypeId);
            if (type != null)
            {
                ad.Type = type;
            }

            ad.Categories = new List<Category>();
            foreach (var categoryId in adBindingModel.Categories)
            {
                var category = this.Data.Categories.FirstOrDefault(c => c.Id == categoryId);
                if (category != null)
                {
                    ad.Categories.Add(category);
                }
            }

            this.Data.Ads.Add(ad);
            this.Data.SaveChanges();

            var adViewModel = this.Data.Ads
                .Where(a => a.Id == ad.Id)
                .Select(AdViewModel.Create)
                .FirstOrDefault();

            return this.Ok(adViewModel);
        }
        public IHttpActionResult CreateAd(CreateAdBindingModel model)
        {
            if (!this.ModelState.IsValid)
            {
                return this.BadRequest(this.ModelState);
            }

            if (model == null)
            {
                return this.BadRequest("No data provided.");
            }

            if (model.Categories.Count() < 1 || model.Categories.Count() > 3)
            {
                return this.BadRequest("Too few or too many categories. Must be between 1 and 3.");
            }

            var ad = new Ad
            {
                Name = model.Name,
                Description = model.Description,
                Price = model.Price,
                PostedOn = DateTime.Now.AddDays(-5),
                Status = AdStatus.Open,
                OwnerId = this.User.Identity.GetUserId(),
                TypeId = model.TypeId
            };

            foreach (var categoryId in model.Categories)
            {
                var category = this.Data.Categories.Find(categoryId);
                if (category == null)
                {
                    return this.BadRequest("Invalid category id.");
                }

                ad.Categories.Add(category);
            }            
           
            this.Data.Ads.Add(ad);
            this.Data.SaveChanges();

            var userId = this.User.Identity.GetUserId();
            var result = this.Data.Ads
                .Where(a => a.Id == ad.Id)
                .Select(a => AdViewModel.Create)
                .FirstOrDefault();

            return this.Ok(result);
        }
        public IHttpActionResult CreateAd(CreateAdBindingModel model)
        {
            var userId = this.User.Identity.GetUserId();
            if (userId == null)
            {
                return this.BadRequest();
            }

            if (!this.ModelState.IsValid)
            {
                return this.BadRequest(this.ModelState);
            }

            var categories = new HashSet<Category>();
            var allCategories = this.Data.Categories;
            foreach (var category in model.Categories)
            {
                var categoryData = allCategories.FirstOrDefault(c => c.Id == category);
                if (categoryData == null)
                {
                    return this.BadRequest("Invalid category");
                }

                categories.Add(categoryData);
            }

            var ad = new Ad()
            {
                Name = model.Name,
                Description = model.Description,
                PostedOn = DateTime.Now,
                TypeId = model.TypeId,
                Price = model.Price,
                OwnerId = userId,
                Categories = categories
            };

            this.Data.Ads.Add(ad);
            this.Data.SaveChanges();

            var result = this.Data.Ads
                .Where(a => a.Id == ad.Id)
                .Select(AdViewModel.Create)
                .FirstOrDefault();

            return this.Ok(result);
        }
        public IHttpActionResult CreateAd([FromBody] CreateAdBindingModel model)
        {
            if (!ModelState.IsValid)
                return BadRequest(ModelState);

            if (!model.Categories.Any())
                return BadRequest("You must specify at least 1 category!");

            if (model.Categories.Count() > 3)
                return BadRequest("You cannot have more than 3 categories!");

            if (!Data.AdTypes.All().Any(ad => ad.Id == model.TypeId))
                return BadRequest("Type with the Id specified doesnt exist!");

            if (Data.Categories.All().Where(c => model.Categories.Contains(c.Id)).ToList().Count !=
                model.Categories.Count())
                return BadRequest("One or more of the specified category Id's doesnt belong to a category!");


            var uId = UserIdProvider.GetUserId();

            var add = new Ad
            {
                Name = model.Name,
                Description = model.Description,
                TypeId = model.TypeId,
                Status = 0,
                PostedOn = DateTime.Now,
                OwnerId = uId,
                Price = model.Price,
                Categories = (Data.Categories.All().Where(c => model.Categories.Contains(c.Id)))
                    .ToList()
            };


            Data.Ads.Add(add);

            Data.SaveChanges();

            var result = Data.Ads.All().Where(d => d.Id == add.Id)
                .Select(AdViewModel.Create)
                .FirstOrDefault();

            return Ok(result);
        }
 public IHttpActionResult CreateAd(CreateAdBindingModel model)
 {
     var userId = this.UserIdProvider.GetUserId();
     var owner = this.Data.Users.Find(userId);
     if (!this.ModelState.IsValid)
     {
         return this.BadRequest(this.ModelState);
     }
     var ad = new Ad()
     {
         Name=model.Name,
         Description=model.Description,
         Type = this.Data.AdTypes.Find(model.TypeId),
         Price = model.Price,
         PostedOn = DateTime.Now,
         Owner = owner,
         Categories= new HashSet<Category>()
     };
     if (model.Categories == null)
     {
         return this.BadRequest("You should enter at least one category!");
     }
     foreach (var id in model.Categories)
     {
         var category = this.Data.Categories.Find(id);
         if (category==null)
         {
             return NotFound();
         }
         ad.Categories.Add(category);
     }
     
     if (ad.Categories.Count > 3)
     {
         return this.BadRequest("The number of categories should not exceed 3!");
     }
     this.Data.Ads.Add(ad);
     this.Data.SaveChanges();
     var result = this.Data.Ads.All()
         .Where(a => a.Id == ad.Id)
         .Select(AdViewModel.Create)
         .FirstOrDefault();
     return this.Ok(result);
 }
        public IHttpActionResult CreateAd(CreateAdBindingModel model)
        {
            string userId = this.UserIdProvider.GetUserId();
            if (userId == null)
            {
                return this.Unauthorized();
            }

            if (model == null)
            {
                return this.BadRequest("Ad model cannot be null.");
            }

            if (!this.ModelState.IsValid)
            {
                return this.BadRequest(this.ModelState);
            }

            var newAd = new Ad
            {
                Name = model.Name,
                Description = model.Description,
                TypeId = model.TypeId,
                Price = model.Price,
                PostedOn = DateTime.Now,
                OwnerId = userId
            };

            foreach (var categoryId in model.Categories)
            {
                var category = this.Data.Categories.Find(categoryId);
                newAd.Categories.Add(category);
            }

            this.Data.Ads.Add(newAd);
            this.Data.SaveChanges();
            var adInDb = this.Data.Ads.All()
                .Where(a => a.Id == newAd.Id)
                .Select(AdViewModel.Create)
                .FirstOrDefault();

            return this.Ok(adInDb);
        }
        public IHttpActionResult CreateAd(CreateAdBindingModel model)
        {
            var userId = this.User.Identity.GetUserId();

            if (model == null)
            {
                return this.BadRequest("Model cannot be null (no data in request)");
            }

            if (!this.ModelState.IsValid)
            {
                return this.BadRequest(this.ModelState);
            }

            var adType = this.Data.AdTypes
                .FirstOrDefault(at => at.Name == model.Type);

            if (adType == null)
            {
                return this.BadRequest("Type is invalid");
            }

            var ad = new Ad()
            {
                Name = model.Name,
                Description = model.Description,
                Price = model.Price,
                Type = adType,
                PostedOn = DateTime.Now,
                Status = AdStatus.Open,
                OwnerId = this.User.Identity.GetUserId()
            };

            this.Data.Ads.Add(ad);
            this.Data.SaveChanges();

            var data = this.Data.Ads
                .Where(a => a.Id == ad.Id)
                .Select(AdViewModel.Create)
                .FirstOrDefault();

            return this.Ok(data);
        }
Example #13
0
        public IHttpActionResult CreateAd(CreateAdBindingModel model)
        {
            var userId = this.User.Identity.GetUserId();

            if (model == null) return this.BadRequest("Invalid user data.");

            // AUTHORIZE ATTRIBUTE CHECK THE SAME
            //if (userId == null) return this.BadRequest("User is not logged in.");

            if (!this.ModelState.IsValid) return this.BadRequest(this.ModelState);

            if (!this.Data.AdTypes.Any(t => t.Id == model.TypeId)) return this.BadRequest("Invalid TypeId");

            var categories = this.Data.Categories.Where(c => model.Categories.Contains(c.Id)).ToList();

            if (categories.Count < 1 || categories.Count > 3) return this.BadRequest("Categories allowed range 1 - 3");

            if (categories.Count != model.Categories.Count()) return this.BadRequest("Invalid category id provided");

            var adCreator = this.Data.Users.Find(userId);
            var adType = this.Data.AdTypes.Find(model.TypeId);

            var ad = new Ad()
            {
                Name = model.Name,
                Description = model.Description,
                Price = model.Price,
                TypeId = model.TypeId,
                Type = adType,
                Categories = categories,
                OwnerId = userId,
                Owner = adCreator,
                PostedOn = DateTime.Now,
                Status = 0
            };

            this.Data.Ads.Add(ad);
            this.Data.SaveChanges();

            var result = this.Data.Ads.Where(a => a.Id == ad.Id).Select(AdViewModel.Create).FirstOrDefault();

            return this.Ok(result);
        }
        public IHttpActionResult CreateAd([FromBody]CreateAdBindingModel model)
        {
            //check if the user is logged-in
            var loggedUser = this.User.Identity.GetUserId();
            var user = this.Context.Users.FirstOrDefault(u => u.Id == loggedUser);
            
            //check if the model is not null

            if (model==null)
            {
                return this.BadRequest("No vlues added!");
            }
            //check if the state of the model is valid
            if (!this.ModelState.IsValid)
            {
                return BadRequest(this.ModelState);
            }

            //validate that thee is at least one Category and mnot more than 3
            if (!model.Categories.Any() || model.Categories.Count() > 3)
            {
                return this.BadRequest("Categories count cannot be " + model.Categories.Count());
            }

            //check if the type ID is real
            var typeId = this.Context.AdTypes
                .FirstOrDefault(c => c.Id == model.TypeId);
            if (typeId == null)
            {
                return this.BadRequest("No type with id " + model.TypeId + " found!");
            }

            //check if the category ID is real

            var categories = new List<Category>();
            foreach (var categoryId in model.Categories)
            {
                var dbCategory = this.Context.Categories.FirstOrDefault(c => c.Id == categoryId);
                if (dbCategory == null)
                {
                    return this.BadRequest("No category with id " + model.TypeId + " found!");
                }

                categories.Add(dbCategory);
            }

            var ad = new Ad()
            {
                Categories = categories,
                Name = model.Name,
                Description = model.Description,
                Type = typeId,
                Price = model.Price,
                Owner = user
           };
            
            this.Context.Ads.Add(ad);
            this.Context.SaveChanges();

            var result = Context.Ads
                .Where(a => a.Id == ad.Id)
                .Select(AdViewModel.Create)
                .FirstOrDefault();

            return this.Ok(result);
        }
        public IHttpActionResult CreateAd(CreateAdBindingModel model)
        {
            var userId = this.UserId;

            if (userId == null) return this.NotFound();

            if (model == null) return this.BadRequest("No data sent");

            if (!this.ModelState.IsValid) return this.BadRequest(this.ModelState);

            if (model.Categories == null || model.Categories.Count() > 3)
            {
                return this.BadRequest("Categories should be atleast 1 and no more than 3");
            }

            if (model.Categories.Select(cat => this.Data.Categories.All().FirstOrDefault(c => c.Id == cat)).Any(category => category == null))
            {
                return this.BadRequest("Categories do not exist");
            }

            if (this.Data.AdTypes.All().FirstOrDefault(t => t.Id == model.TypeId) == null)
            {
                return this.BadRequest("Unknown or missing type of ad");
            }

            ICollection<Category> categories = new HashSet<Category>();

            foreach (var cat in model.Categories)
            {
                categories.Add(this.Data.Categories.Find(cat));
            }

            var ad = new Ad()
            {
                Name = model.Name,
                Description = model.Description,
                TypeId = model.TypeId,
                Price = model.Price,
                Categories = categories,
                OwnerId = userId.GetUserId(),
                PostedOn = DateTime.Now,
                Status = AdStatus.Open
            };

            this.Data.Ads.Add(ad);
            this.Data.SaveChanges();

            return this.Ok(ad.Id);
        }
        public IHttpActionResult CreateAd(CreateAdBindingModel model)
        {
            if (model == null)
            {
                return this.BadRequest("Model cannot be empty!");
            }

            if (!this.ModelState.IsValid)
            {
                return this.BadRequest(this.ModelState);
            }

            var type = Data.AdTypes.All().FirstOrDefault(t => t.Id == model.TypeId);
            if (type == null)
            {
                return this.BadRequest("There is no ad type with such id!");
            }

            int numOfCategories = model.Categories.Count();
            if (numOfCategories < 1 || numOfCategories > 3)
            {
                return this.BadRequest("The number of categories should be between 1 and 3");
            }

            List<Category> categories = new List<Category>();
            foreach (var catId in model.Categories)
            {
                var category = Data.Categories.All().FirstOrDefault(c => c.Id == catId);
                if (category == null)
                {
                    string message = string.Format("There is no category with such id: {0}!", catId);
                    return this.BadRequest(message);
                }
                categories.Add(category);
            }

            var userId = this.UserIdProvider.GetUserId();

            var ad = new Ad()
            {
                Name = model.Name,
                Description = model.Description,
                PostedOn = DateTime.Now,
                TypeId = model.TypeId,
                OwnerId = userId,
                Price = model.Price,
                Categories = categories,

            };

            Data.Ads.Add(ad);
            Data.SaveChanges();

            var result = this.Data.Ads.All()
                .Where(a => a.Id == ad.Id)
                .Select(AdViewModel.Create)
                .FirstOrDefault();

            return this.Ok(result);
        }
        public IHttpActionResult PostAd(CreateAdBindingModel model)
        {
            var userId = this.UserIdProvider.GetUserId();

            if (userId == null)
            {
                return this.BadRequest("Invalid session token.");
            }
            if (model == null)
            {
                return this.BadRequest("Invalid input data");
            }
            if (!this.ModelState.IsValid)
            {
                return this.BadRequest(ModelState);
            }

            var categories = new List<Category>();
            if (model.Categories == null)
            {
                return this.BadRequest("You must insert categories");
            }
            if (!model.Categories.Any() || model.Categories.Count() > 3)
            {
                return this.BadRequest("The categories count must be between 1-3");
            }
            foreach (var categorie in model.Categories)
            {
                var categorieToAdd = this.Data.Categories.All().FirstOrDefault(c=>c.Id.ToString()==categorie);
                if (categorieToAdd == null)
                {
                    return this.BadRequest("Invalid categories id ");
                }
                categories.Add(categorieToAdd);
            }
           
            if (!this.Data.AdTypes.All().Any(a => a.Id.ToString() == model.TypeId))
            {
                return this.BadRequest("invalid type id .");
            }

            Ad newAd = new Ad()
            {
                Name = model.Name,
                Categories = categories,
                PostedOn = DateTime.Now,
                Description = model.Description,
                OwnerId = userId,
                Price = model.Price,
                Status = AdStatus.Open,
                TypeId = Convert.ToInt32(model.TypeId)
            };

            this.Data.Ads.Add(newAd);
            this.Data.SaveChanges();
            return this.Ok(this.Data.Ads.All().Where(a=>a.Id==newAd.Id).Select(AdsViewModel.Create));
        }
        public IHttpActionResult CreateAds(CreateAdBindingModel model)
        {
            var UserId = this.User.Identity.GetUserId();
            if (UserId == null)
            {
                return this.Unauthorized();
            }

            if (!this.ModelState.IsValid)
            {
                return this.BadRequest(ModelState);
            }

            if (model == null)
            {
                return this.BadRequest("Model cannot be empty!");
            }

            if (!model.Categories.Any() || model.Categories.Count() > 3)
            {
                return this.BadRequest("Categories count cannot be " + model.Categories.Count());
            }

            var adTypeDb = this.Data.AdTypes.FirstOrDefault(a => a.Id == model.TypeId);

            if (adTypeDb == null)
            {
                return this.BadRequest("No type with id " + model.TypeId + " found!");
            }

            var categoris = new List<Category>();

            foreach (var catId in model.Categories)
            {
                var categoryFromDB = this.Data.Categories.Find(catId);

                if (categoryFromDB == null)
                {
                    return this.BadRequest("No category with id " + catId + " found!");
                }

                categoris.Add(categoryFromDB);
            }

            var user = this.Data.Users.Find(UserId);

            var adToAdd = new Ad()
            {
                Categories = categoris,
                Description = model.Description,
                Name = model.Name,
                Price = model.Price,
                TypeId = model.TypeId,
                PostedOn = DateTime.Now,
                Owner = user,
                Type = adTypeDb
            };

            this.Data.Ads.Add(adToAdd);
            this.Data.SaveChanges();

            var result = this.Data.Ads
                .Where(a => a.Id == adToAdd.Id)
                .Select(AdsViewModel.Create)
                .FirstOrDefault();

            return this.Ok(result);

        }
Example #19
0
        public IHttpActionResult Post(CreateAdBindingModel model)
        {
            if (!this.ModelState.IsValid)
            {
                return this.BadRequest(this.ModelState);
            }

            if (model == null)
            {
                return this.BadRequest("No data sent.");
            }

            if (model.Categories.Count() < 1 || 3 < model.Categories.Count())
            {
                return this.BadRequest("The number of categories should be in the range [1...3]");
            }

            if (model.TypeId < 1 || 3 < model.TypeId)
            {
                return this.BadRequest("The type id should be in the range [1...3]");
            }

            foreach (var categoryId in model.Categories)
            {
                if (categoryId < 1 || 6 < categoryId)
                {
                    return this.BadRequest("All category ids should be in the range [1...6]");
                }
            }

            var categories = new HashSet<Category>();
            foreach (var categoryId in model.Categories)
            {
                var category = ConvertToCategory(categoryId);

                categories.Add(category);
            }


            var loggedUserId = this.UserIdProvider.GetUserId();
            var ad = new Ad()
            {
                Name = model.Name,
                Description = model.Description,
                TypeId = model.TypeId,
                Price = model.Price,
                PostedOn = DateTime.Now,
                OwnerId = loggedUserId,
                Categories = categories,
                Status = AdStatus.Open
            };

            this.Data.Ads.Add(ad);
            this.Data.SaveChanges();

            var result = this.Data.Ads.All()
                .Where(a => a.Id == ad.Id)
                .Select(AdViewModel.Create)
                .FirstOrDefault();

            return this.Ok(result);
        }
Example #20
0
        public IHttpActionResult CreateAd(CreateAdBindingModel model)
        {
            var UserId = this.User.Identity.GetUserId();
            if (UserId == null)
            {
                return this.BadRequest("User need to be logged on!!!");
            }

            if (!this.ModelState.IsValid)
            {
                return this.BadRequest(this.ModelState);
            }

            if (model == null)
            {
                return this.BadRequest("No data entry");
            }

            if (model.Categories.Count() > 3 || model.Categories.Count() < 0)
            {
                return this.BadRequest("categories can be no more than 3");

            }

            var cat = new List<Category>();
            foreach (var catId in model.Categories)
            {
                var DbCategories = Data.Categories
                    .FirstOrDefault(c => c.Id == catId);

                cat.Add(DbCategories);

                if (DbCategories == null)
                {
                    return this.BadRequest("this category does not exist");
                }
            }

            var ad = new Ad()
            {
                OwnerId = UserId,
                Name = model.Name,
                PostedOn = model.PostedOn,
                Description = model.Description,
                Price = model.Price,
                TypeId = model.TypeId,
                Categories = cat
            };

            this.Data.Ads.Add(ad);
            this.Data.SaveChanges();

            var result = this.Data.Ads.Where(a => a.Id == ad.Id)
             .Select(AdViewModel.Create).FirstOrDefault();

            return this.Ok(result);
        }
        public IHttpActionResult AddAd([FromBody]CreateAdBingindModel model)
        {
            var userId = this.User.Identity.GetUserId();

            if (!this.ModelState.IsValid)
            {
                return this.BadRequest(this.ModelState);
            }

            if (!model.Categories.Any())
            {
                return this.BadRequest("Please supply categories");
            }

            if (model.Categories.Count() > 3)
            {
                return this.BadRequest("Please specify no more than 3 categories");
            }

            var type = this.Context.AdTypes.FirstOrDefault(at => at.Id == model.TypeId);

            if (type == null)
            {
                return this.BadRequest("Invalid type id");
            }

            Category category;
            List<Category> categories = new List<Category>();
            foreach (int categoryId in model.Categories)
            {
                category = this.Context.Categories.FirstOrDefault(c => c.Id == categoryId);

                if (category == null)
                {
                    return this.BadRequest(string.Format("Category id {0} is invalid.", categoryId));
                }

                categories.Add(category);
            }

            var ad = new Ad()
            {
                Categories = categories,
                ClosedOn = null,
                Description = model.Description,
                Name = model.Name,
                OwnerId = userId,
                Price = model.Price,
                PostedOn = DateTime.Now,
                Status = AdStatus.Open,
                TypeId = model.TypeId
            };

            this.Context.Ads.Add(ad);

            try
            {
                this.Context.SaveChanges();

                var newEntry = this.Context.Ads
                    .Where(a => a.Id == ad.Id)
                    .Select(AdViewModel.Project)
                    .FirstOrDefault();

                return this.Ok(newEntry);
            }
            catch (Exception e)
            {
                return this.InternalServerError(e);
            }
        }
        public IHttpActionResult CreateAd(CreateAdBindingModel model)
        {
            if (model == null)
            {
                return this.BadRequest("Model cannot be null");
            }

            if (!this.ModelState.IsValid)
            {
                return this.BadRequest(this.ModelState);
            }

            var currentUserId = this.UserIdProvider.GetUserId();    // Check if id is correct
            var adType = this.Data.AdTypes.Find(model.TypeId);
            if (adType == null)
            {
                return this.BadRequest("AdType with such id does not exists");
            }

            var ad = new Ad
                     {
                         Name = model.Name,
                         Description = model.Description,
                         Price = model.Price,
                         PostedOn = DateTime.Now,
                         Status = AdStatus.Open,
                         TypeId = adType.Id,
                         OwnerId = currentUserId
                     };

            if (model.Categories.Count() > 3 || model.Categories == null)
            {
                return this.BadRequest("Categories must be in range [1..3]");
            }

            foreach (var categoryId in model.Categories)
            {
                var category = this.Data.Categories.All().FirstOrDefault(c => c.Id == categoryId);
                if (category == null)
                {
                    return this.BadRequest("Invalid category id.");
                }

                ad.Categories.Add(category);
            }

            this.Data.Ads.Add(ad);
            this.Data.SaveChanges();
            var result =
                this.Data.Ads.All().Where(a => a.Id == ad.Id).Select(AllAdsViewModel.Create).FirstOrDefault();

            return this.Ok(result);
        }
        public IHttpActionResult Create(AdBindingModel model)
        {
            if (model == null)
            {
                return this.BadRequest("No data to create the ad with.");
            }

            if (!this.ModelState.IsValid)
            {
                return this.BadRequest(this.ModelState);
            }

            if (model.Categories == null)
            {
                return this.BadRequest("Ad categories are required.");
            }

            if (model.Categories.Count() < 1 || model.Categories.Count() > 3)
            {
                return this.BadRequest("Ad categories count must be between 1 and 3.");
            }

            var currnetUserId = this.UserProvider.GetUserId();
            var newAd = new Ad
            {
                Name = model.Name,
                Description = model.Description,
                Price = model.Price,
                PostedOn = DateTime.Now,
                OwnerId = currnetUserId,
                Categories = new HashSet<Category>()
            };

            foreach (var categoryId in model.Categories)
            {
                var existingCategory = this.Data.Categories.Find(categoryId);
                if (existingCategory == null)
                {
                    return this.BadRequest("No category with the specified Id.");
                }

                newAd.Categories.Add(existingCategory);
            }

            var existingType = this.Data.AdTypes.Find(model.TypeId);
            if (existingType == null)
            {
                return this.BadRequest("No adtype with the specified id.");
            }

            newAd.Type = existingType;
            newAd.TypeId = existingType.Id;

            this.Data.Ads.Add(newAd);
            this.Data.SaveChanges();

            var adViewModel = this.Data.Ads.All()
                .Where(a => a.Id == newAd.Id)
                .Select(AdViewModel.ToViewModel)
                .FirstOrDefault();

            return this.Ok(adViewModel);
        }