Example #1
0
        public async Task <IActionResult> PutBlog([FromRoute] int id, [FromBody] Blog blog)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

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

            _context.Entry(blog).State = EntityState.Modified;

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

            return(NoContent());
        }
Example #2
0
        public async Task <Post> AddAsync(Post post)
        {
            var createdPost = await _context.Posts.AddAsync(post);

            await _context.SaveChangesAsync();

            return(createdPost.Entity);
        }
Example #3
0
        public async Task <Attachment> AddAsync(Attachment attachment)
        {
            var createdAttachment = await _context.Attachments.AddAsync(attachment);

            await _context.SaveChangesAsync();

            return(createdAttachment.Entity);
        }
Example #4
0
        public async Task <Picture> AddAsync(Picture picture)
        {
            var createdPicture = await _context.Pictures.AddAsync(picture);

            await _context.SaveChangesAsync();

            return(createdPicture.Entity);
        }
Example #5
0
        public async Task <bool> InsertUser(string name)
        {
            user = new User {
                name = name
            };
            dbcontext.Add(user);                              // tracking user object

            var isSaved = await dbcontext.SaveChangesAsync(); // insert into DB

            //dbcontext.Entry(user).State = EntityState.Detached; // remove user entity from tracking/memory

            return(isSaved >= 1 ? true : false);
        }
Example #6
0
        public async Task <IActionResult> PutBlog(int id, BlogModel blog)
        {
            if (id != blog.Id)
            {
                return(BadRequest());
            }

            var originalBlog = await GetUserBlog(blog.Id);

            if (originalBlog != null)
            {
                originalBlog.Body    = blog.Body;
                originalBlog.Subject = blog.Subject;
                _context.Entry(originalBlog).State = EntityState.Modified;
            }
            else
            {
                return(NotFound());
            }

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

            return(NoContent());
        }