public async Task <IActionResult> PutStudent([FromRoute] int id, [FromBody] Student student)
        {
            if (id != student.Id)
            {
                return(BadRequest());
            }

            context.Entry(student).State = EntityState.Modified;

            try
            {
                await context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!StudentExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
Пример #2
0
        public async Task <IActionResult> PutBlog(int id, Blog blog)
        {
            if (id != blog.BlogId)
            {
                return(BadRequest());
            }

            //_context.Entry(blog).State = EntityState.Modified;
            _context.Entry(blog).State        = EntityState.Detached;
            _context.Entry(blog.Detail).State = EntityState.Detached;


            try
            {
                //await _context.SaveChangesAsync();
                _context.Update(blog);
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!BlogExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
        public async Task <ActionResult <BlogPostItem> > PostBlogPostItem(BlogPostItem item)
        {
            _context.BlogPostItems.Add(item);
            await _context.SaveChangesAsync();

            return(CreatedAtAction(nameof(BlogPostItem), new { id = item.Id }, item));
        }
Пример #4
0
        //public async Task<IActionResult> Create([Bind("BlogId,Name,Url,Detail_Author,Detail_ISBN")] Blog blog)
        public async Task <IActionResult> Create([Bind("BlogId,Name,Url,Detail")] Blog blog)
        {
            if (ModelState.IsValid)
            {
                _context.Add(blog);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(blog));
        }
Пример #5
0
        public async Task <BlogPostViewModelResponse> AddPost(BlogPostTagViewModelRequest post)
        {
            Models.BlogPost blogPost = new Models.BlogPost();
            blogPost.Tags = new List <BlogPostTag>();
            Tag tag = new Tag();
            BlogPostViewModelResponse response = new BlogPostViewModelResponse();
            BlogPostTag blogPostTag            = new BlogPostTag();

            tag.BlogPosts = new List <BlogPostTag>();

            if (db != null)
            {
                //get generated slug
                string slug = Helpers.GenerateSlug.ToUrlSlug(post.BlogPost.Title);
                //blogPost.Id - automatic generated
                blogPost.Slug        = response.Slug = slug;
                blogPost.Title       = response.Title = post.BlogPost.Title;
                blogPost.Description = response.Description = post.BlogPost.Description;
                blogPost.Body        = response.Body = post.BlogPost.Body;
                blogPost.CreatedAt   = response.CreatedAt = DateTime.Now;
                blogPost.UpdatedAt   = response.UpdatedAt = DateTime.Now;
                response.tagList     = post.BlogPost.Tags;

                //checking tags
                List <string> list = await GetTags();

                //records exist in new list /not in db list
                List <string> dList = post.BlogPost.Tags.Except(list).ToList();
                //add new records in Tag table
                for (int i = 0; i < dList.Count; i++)
                {
                    string name = dList.ElementAt(i);
                    tag.Name = name;


                    blogPostTag.BlogPost = blogPost;
                    blogPostTag.Tag      = tag;
                    blogPostTag.BlogPost.Tags.Add(blogPostTag);
                    //db.SaveChanges();
                }
                await db.Tags.AddAsync(tag);

                await db.BlogPosts.AddAsync(blogPost);

                await db.BlogPostTag.AddAsync(blogPostTag);

                await db.SaveChangesAsync();

                return(response);
            }
            return(response);
        }
Пример #6
0
 public async Task SaveAsync()
 {
     await Context.SaveChangesAsync();
 }
Пример #7
0
        public async Task AddAsync(BlogPost post)
        {
            await _postsDbContext.BlogPosts.AddAsync(post);

            await _postsDbContext.SaveChangesAsync();
        }