Beispiel #1
0
        public async Task <IActionResult> Edit(int id, [Bind("Id,WordCount,Text,PostedOn")] Blog blog)
        {
            if (id != blog.Id)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(blog);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!BlogExists(blog.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            return(View(blog));
        }
Beispiel #2
0
        public async Task <IActionResult> Edit(int id, [Bind("Id,Nickname,Description,PostedOn,PostId")] Comment comment)
        {
            if (id != comment.Id)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(comment);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!CommentExists(comment.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            ViewData["PostId"] = new SelectList(_context.Posts, "Id", "AppUserId", comment.PostId);
            return(View(comment));
        }
Beispiel #3
0
        public async Task <IActionResult> Edit(int id, [Bind("Id,Name,Description")] Category category)
        {
            if (id != category.Id)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(category);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!CategoryExists(category.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            return(View(category));
        }
Beispiel #4
0
        public async Task <Response> UpdateAsync(string slug, UpdatePostRequest request)
        {
            var query = _context.Posts.AsQueryable();

            query = ApplyInclude(query);

            var post = await query.SingleOrDefaultAsync(i => i.Slug == slug);

            if (post == null)
            {
                return(null);
            }

            _context.Posts.Attach(post);
            _context.Posts.Update(post);

            _mapper.Map(request.BlogPost, post);

            _context.Update(post);
            await _context.SaveChangesAsync();

            var response = _mapper.Map <PostModel>(post);

            return(new PostResponse
            {
                BlogPost = response
            });
        }
Beispiel #5
0
        public async Task <IActionResult> Edit(int id, [Bind("ID,Name,Content,AuthorEmail")] BgModel bgModel)
        {
            if (id != bgModel.ID)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(bgModel);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!BgModelExists(bgModel.ID))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            return(View(bgModel));
        }
        public async Task <IActionResult> Edit(int id, [Bind("ID,BlogId,Title,Content,Date")] Post post)
        {
            if (id != post.ID)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(post);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!PostExists(post.ID))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            ViewData["BlogId"] = new SelectList(_context.Set <Blog>(), "ID", "ID", post.BlogId);
            return(View(post));
        }
Beispiel #7
0
        public async Task <IActionResult> Edit(int id, [Bind("Id,Title,Content,CreatedAt,AutorEmail")] Blog blog) //Blog ist der bearbeitete Datensatz
        {
            if (id != blog.Id)
            {
                return(NotFound());
            }

            if (ModelState.IsValid) //Ist der Datensatz nach der modifizierung noch Valide?
            {
                try
                {
                    _context.Update(blog);             //Markiere, dass ein Datenatz modifiziert wird
                    await _context.SaveChangesAsync(); //Modifizerungen werden in DB übertragen
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!BlogExists(blog.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            return(View(blog));
        }
Beispiel #8
0
        public async Task <IActionResult> Edit(int id, [Bind("Id,Title,Description,Published,Modified,AppUserId,CommentId")] Post post)
        {
            if (id != post.Id)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(post);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!PostExists(post.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            ViewData["AppUserId"] = new SelectList(_context.AppUsers, "Id", "Id", post.AppUserId);
            return(View(post));
        }
        public async Task <IActionResult> Edit(int id, [Bind("Id,Title,Content,CreatedAt,CreatedBy")] Blog blog)
        {
            if (id != blog.Id)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    //_context.Attach(Blog).State = EntityState.Modified; = #ältere Schreibvariante
                    _context.Update(blog);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!BlogExists(blog.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            return(View(blog));
        }
Beispiel #10
0
        public async Task <IActionResult> Edit(int id, [Bind("Id,Title,Content,DateTime,UserId")] Publication publication)
        {
            if (id != publication.Id)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(publication);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!PublicationExists(publication.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            return(View(publication));
        }
        public async Task <IActionResult> Edit(int id, [Bind("Id,Pacc,SubmissionDate,Address1,Address2,City,State,ZIP,Phone,AddressID,IssueType,ComplaintDescription,PublicNotes,PrivateNotes,HumanSafety,Notes")] Complaint complaint)
        {
            if (id != complaint.Id)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(complaint);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!ComplaintExists(complaint.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            return(View(complaint));
        }
Beispiel #12
0
 public async Task UpdatePost(int Id, Post post)
 {
     if (Id == post.ID)
     {
         _context.Update(post);
         await _context.SaveChangesAsync();
     }
 }
        public async Task <IActionResult> Edit(int id, EntryViewModel vm)
        {
            var entry = vm.Entry;

            if (id != entry.Id)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    entry.ModifiedOn = DateTime.UtcNow;
                    _ctx.Update(entry);
                    await _ctx.SaveChangesAsync();

                    var updatedCategoryIds = vm.Categories.Where(c => c.Id != 0).Select(s => s.Id);
                    var entryCats          = _ctx.BlogEntryCategories.Where(w => w.EntryId == entry.Id);

                    foreach (var ec in entryCats)
                    {
                        if (!updatedCategoryIds.Contains(ec.CategoryId))
                        {
                            _ctx.BlogEntryCategories.Remove(ec);
                        }
                    }
                    await _ctx.SaveChangesAsync();

                    foreach (var categoryId in updatedCategoryIds)
                    {
                        if (!entryCats.Any(a => a.CategoryId == categoryId))
                        {
                            _ctx.BlogEntryCategories.Add(new EntryCategory(entry.Id, categoryId));
                        }
                    }
                    await _ctx.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!EntryExists(entry.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction("Admin"));
            }
            return(View(vm));
        }
Beispiel #14
0
        public void UpdateBlogPostSettings(string blogPostId, bool isPrivate, bool commentsAllowed = false, bool commentsModerated = false)
        {
            // NOTE: Comment Settings are not versioned, so is okay to update the model directly
            var existingBlogPost = _blogDbContext.BlogPosts.FirstOrDefault(x => x.Id == blogPostId);

            if (existingBlogPost == null)
            {
                throw new NullReferenceException($"Could not update Blog Post settings: Id {blogPostId} not found");
            }


            existingBlogPost.IsPrivate = isPrivate;

            // TODO - Simplify comments settings - no need for Private vs Public since
            //        "private" blog posts would only be viewed by authorized users anyway
            existingBlogPost.PrivateCommentsAllowed   = commentsAllowed;
            existingBlogPost.PublicCommentsAllowed    = commentsAllowed;
            existingBlogPost.PublicCommentsModerated  = commentsModerated;
            existingBlogPost.PrivateCommentsModerated = commentsModerated;

            _blogDbContext.Update(existingBlogPost);
            _blogDbContext.SaveChanges();
        }
        public async Task <ActionResult <User> > Put(User user)
        {
            if (user == null)
            {
                return(BadRequest());
            }
            if (!db.Users.Any(x => x.Id == user.Id))
            {
                return(NotFound());
            }

            db.Update(user);
            await db.SaveChangesAsync();

            return(Ok(user));
        }
Beispiel #16
0
        public async Task <ActionResult <Post> > Put(Post post)
        {
            if (post == null)
            {
                return(BadRequest());
            }
            if (!db.Posts.Any(x => x.Id == post.Id))
            {
                return(NotFound());
            }

            db.Update(post);
            await db.SaveChangesAsync();

            return(Ok(post));
        }
Beispiel #17
0
        public async Task <IActionResult> Edit(int id, [Bind("Id,Title,Content,CategoryId")] Post post)
        {
            if (id != post.Id)
            {
                return(NotFound());
            }

            var oldPost = await _context.Posts.FindAsync(id);

            if (oldPost == null)
            {
                return(NotFound());
            }

            oldPost.Slug       = _slugger.Sluggify(post.Title);
            oldPost.Updated    = DateTime.Now;
            oldPost.CategoryId = post.CategoryId;
            oldPost.Title      = post.Title;
            oldPost.Content    = post.Content;

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(oldPost);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!PostExists(post.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            ViewData["CategoryId"] = new SelectList(_context.Categories, "Id", "Title", post.CategoryId);
            return(View(post));
        }
Beispiel #18
0
        public async Task <IActionResult> EditRole(string id, RoleViewModel model)
        {
            if (ModelState.IsValid)
            {
                bool isExist = !String.IsNullOrEmpty(id);
                if (!isExist)
                {
                    return(NotFound());
                }
                var role = await _roleManager.FindByIdAsync(id);

                role.Name        = model.Name;
                role.Description = model.Description;
                _dbContext.Update(role);
                await _dbContext.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(model));
        }
Beispiel #19
0
        public async Task <IActionResult> AddOrUpdate(int postId, [FromForm] EditablePost newPost)
        {
            bool isNew = postId == 0;

            if (isNew && _blogContext.Posts.Any(c => c.UrlSegment == newPost.UrlSegment))
            {
                ModelState.AddModelError("Blog.ViewModels.NewPost.Permalink", string.Format("Url Segment {0} already exists", newPost.UrlSegment));
            }

            if (ModelState.IsValid)
            {
                var dbPost = _mapper.Map <Data.Models.Post>(newPost);
                dbPost.LastModifiedDate = DateTime.Today;
                _blogContext.Update(dbPost); // Performs add or update
                await _blogContext.SaveChangesAsync();

                return(Ok(isNew ? "Post Added" : "Post Updated"));
            }
            return(BadRequest(ModelState));
        }
Beispiel #20
0
        public async Task <IActionResult> Edit(int id, [Bind("Id,Title")] Category category)
        {
            if (id != category.Id)
            {
                return(NotFound());
            }

            var oldCategory = await _context.Categories.FindAsync(id);

            if (category == null)
            {
                return(NotFound());
            }
            oldCategory.Updated = DateTime.Now;
            oldCategory.Title   = category.Title;
            oldCategory.Slug    = _slugger.Sluggify(category.Title);

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(oldCategory);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!CategoryExists(category.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            return(View(category));
        }
Beispiel #21
0
 public async Task Update(TEntity entity)
 {
     dbContext.Update(entity);
     await dbContext.SaveChangesAsync();
 }
Beispiel #22
0
        public async Task <IActionResult> Edit(int id, [Bind("ProductId,Name,Description,Price,DateCreated,DateUpdated,ProductPicture")] Product product)
        {
            if (id != product.ProductId)
            {
                return(NotFound());
            }

            if (SelectedCategories.Length == 0)
            {
                ModelState.AddModelError(String.Empty, "Phải ít nhất một loại sản phẩm");
            }

            if (ModelState.IsValid)
            {
                // Lấy nội dung từ DB
                var productUpdate = await _context.Products.Where(p => p.ProductId == id)
                                    .Include(p => p.ProductCategories)
                                    .ThenInclude(c => c.Category).FirstOrDefaultAsync();

                if (productUpdate == null)
                {
                    return(NotFound());
                }

                productUpdate.Name           = product.Name;
                productUpdate.Description    = product.Description;
                productUpdate.Price          = product.Price;
                productUpdate.ProductPicture = product.ProductPicture;
                productUpdate.DateUpdated    = DateTime.Now;

                // Các danh mục không có trong selectedCategories
                var listcateremove = productUpdate.ProductCategories
                                     .Where(p => !SelectedCategories.Contains(p.CategoryID))
                                     .ToList();
                listcateremove.ForEach(c => productUpdate.ProductCategories.Remove(c));

                // Các ID category chưa có trong postUpdate.PostCategories
                var listCateAdd = SelectedCategories
                                  .Where(
                    id => !productUpdate.ProductCategories.Where(c => c.CategoryID == id).Any()
                    ).ToList();

                listCateAdd.ForEach(id => {
                    productUpdate.ProductCategories.Add(new ProductCategory()
                    {
                        ProductID  = productUpdate.ProductId,
                        CategoryID = id
                    });
                });

                try
                {
                    _context.Update(productUpdate);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!ProductExists(product.ProductId))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            var categories = await _context.Categories.ToListAsync();

            ViewData["categories"] = new MultiSelectList(categories, "Id", "Name", SelectedCategories);
            return(View(product));
        }
Beispiel #23
0
 public async Task UpdateAsync(T entity)
 {
     using var context = new BlogDbContext();
     context.Update(entity);
     await context.SaveChangesAsync();
 }