Пример #1
0
        public async Task <IHttpActionResult> PostStory(StoryVM storyVM)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            Story story = new Story();

            ConvertStoryVM(ref story, storyVM);
            foreach (var genreVM in storyVM.Genres)
            {
                Genre genre = await db.Genres.FindAsync(genreVM.GenreId);

                story.Genres.Add(genre);
            }

            db.Stories.Add(story);
            await db.SaveChangesAsync().ConfigureAwait(false);

            //db.SaveChanges();

            storyVM.Update(story);

            return(CreatedAtRoute("NameApi", new { slug = storyVM.Slug }, storyVM));
        }
Пример #2
0
        public async Task <IHttpActionResult> PutStory(int id, StoryVM storyVM)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != storyVM.StoryId)
            {
                return(BadRequest());
            }

            Story story = db.Stories.Find(id);

            ConvertStoryVM(ref story, storyVM);
            foreach (var genreVM in storyVM.Genres)
            {
                Genre genre = await db.Genres.FindAsync(genreVM.GenreId);

                story.Genres.Add(genre);
            }

            db.Entry(story).State = EntityState.Modified;

            try
            {
                //db.SaveChanges();
                await db.SaveChangesAsync().ConfigureAwait(false);
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!StoryExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            storyVM.Update(story);

            return(StatusCode(HttpStatusCode.OK));
        }