public async Task <IActionResult> CreateItem([FromBody] CarouselItemCreateEditModel item) { return(await this.Execute(true, true, async() => { string carouselItemId = await this.homeContent.CreateCarouselItem(item); return this.Ok(new { carouselItemId = carouselItemId }); })); //if (!this.IsInRole("admin")) //{ // return this.StatusCode(StatusCodes.Status401Unauthorized); //} //if (!ModelState.IsValid) //{ // return this.BadRequest(ModelState); //} //try //{ // string carouselItemId = await this.homeContent.CreateCarouselItem(item); // return this.Ok(new { carouselItemId = carouselItemId }); //} //catch (Exception e) //{ // return this.StatusCode(StatusCodes.Status400BadRequest, e.Message); //} }
public async Task <string> CreateCarouselItem(CarouselItemCreateEditModel data) { if (string.IsNullOrEmpty(data.ImageUrl)) { throw new ArgumentException("ImageUrl cannot be an empty string"); } CarouselItem carouselItem = new CarouselItem { ImageUrl = data.ImageUrl, Heading = data.Heading, Content = data.Content }; await this.db.CarouselItems.AddAsync(carouselItem); await this.db.SaveChangesAsync(); return(carouselItem.Id); }
public async Task <string> EditCarouselItem(string id, CarouselItemCreateEditModel data) { if (string.IsNullOrEmpty(data.ImageUrl)) { throw new ArgumentException("ImageUrl cannot be an empty string"); } if (!await this.db.CarouselItems .AnyAsync(i => i.Id == id)) { throw new ArgumentException("Item with the given id does not exist."); } CarouselItem item = await this.db.CarouselItems .FirstOrDefaultAsync(i => i.Id == id); item.ImageUrl = data.ImageUrl; item.Heading = data.Heading; item.Content = data.Content; await this.db.SaveChangesAsync(); return(item.Id); }