public ProductCategoryPageDTO GetProductCategoryPage(string categoryAlias, int pageNumber, int pageSize, ProductOrderType orderType) { Expression <Func <Product, object> > orderExp; switch (orderType) { case ProductOrderType.Name: orderExp = p => p.Name; break; case ProductOrderType.Price: orderExp = p => p.Price; break; default: orderExp = p => p.Name; break; } ProductCategory productCategory = _dbContext.ProductCategories.Include(pc => pc.Page) .FirstOrDefault(pc => pc.Page.Alias == categoryAlias); if (productCategory == null || productCategory.Page == null) { return(null); } //выполняется в отдельном зпросе так как невозможно сортировать и выборочно получать продукты в запросе, получающем категорию товаров IEnumerable <Product> products = _dbContext.Entry(productCategory).Collection(pc => pc.ProductToCategories).Query() .Include(ptc => ptc.Product).ThenInclude(p => p.MainImage).Include(ptc => ptc.Product).ThenInclude(p => p.Page) .Select(ptc => ptc.Product).OrderBy(orderExp).Skip((pageNumber - 1) * pageSize).Take(pageSize); IMapper categoryMapper = new MapperConfiguration(cfg => { cfg.CreateMap <ProductCategory, ProductCategoryPageDTO>() .ForMember(catDTO => catDTO.Alias, opt => opt.MapFrom(cat => cat.Page.Alias)) .ForMember(catDTO => catDTO.Description, opt => opt.MapFrom(cat => cat.Page.Description)) .ForMember(catDTO => catDTO.Title, opt => opt.MapFrom(cat => cat.Page.Title)); }).CreateMapper(); IMapper productMapper = new MapperConfiguration(cfg => { cfg.CreateMap <Product, ProductPageDTO>() .ForMember(prodDTO => prodDTO.Alias, opt => opt.MapFrom(prod => prod.Page.Alias)); cfg.CreateMap <Image, ImageDTO>(); }).CreateMapper(); ProductCategoryPageDTO productCategoryDTO = categoryMapper.Map <ProductCategoryPageDTO>(productCategory); IEnumerable <ProductPageDTO> productsDTO = productMapper.Map <IEnumerable <ProductPageDTO> >(products); productCategoryDTO.Products = productsDTO; productCategoryDTO.TotalItems = _dbContext.Entry(productCategory).Collection(pc => pc.ProductToCategories).Query().Count(); return(productCategoryDTO); }
// GET: Admin/DeletePost public ActionResult DeletePost(int id) { using (var context = new SiteDbContext()) { Articles post = context.Articles.Find(id); string path = System.IO.Path.Combine(Server.MapPath("~/Images/Uploads/Articles/Header/"), post.Id.ToString()); if (System.IO.File.Exists(path + "x1280.jpeg")) { System.IO.File.Delete(path + "x1280.jpeg"); } if (System.IO.File.Exists(path + "x800.jpeg")) { System.IO.File.Delete(path + "x800.jpeg"); } if (System.IO.File.Exists(path + "x320.jpeg")) { System.IO.File.Delete(path + "x320.jpeg"); } context.Entry(post).State = System.Data.Entity.EntityState.Deleted; context.SaveChanges(); } UpdateSitemapXML(); return(RedirectToAction("Articles", "Admin")); }
public ActionResult NewsletterUnsubscribe(string email, string guid) { email = HttpUtility.UrlDecode(email); guid = HttpUtility.UrlDecode(guid); using (var db = new SiteDbContext()) { var ifEmailExist = db.NewsletterRegistration .Any(r => r.Email.Equals(email)); if (ifEmailExist) { var userData = db.NewsletterRegistration.Where(r => r.Email.Equals(email)).FirstOrDefault(); if (userData.Email == email && userData.SecretKey == guid) { NewsletterRegistration newsletterAccount = db.NewsletterRegistration.Find(userData.Id); db.Entry(newsletterAccount).State = System.Data.Entity.EntityState.Deleted; db.SaveChanges(); ViewBag.Message = "Vous êtes maintenant désinscrit de la newsletter de Gamers Addict."; } else { ViewBag.Message = "Erreur dans clef secrète."; } } else { ViewBag.Message = "Vous n'êtes pas inscrit à la newsletter, désinscription impossible."; } } return(View()); }
public async Task <IActionResult> PutContent([FromRoute] int id, [FromBody] Content content) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != content.ContentId) { return(BadRequest()); } _context.Entry(content).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!ContentExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public ActionResult Edit(Review review, int MovieID) { db.Configuration.ValidateOnSaveEnabled = false; db.Entry(review).State = EntityState.Modified; db.SaveChanges(); Movie movie = db.Movies.Find(MovieID); var avg = movie.Reviews.Average(r => r.Rating); movie.Rate = (int)avg; db.Entry(movie).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Details/" + MovieID, "Movie")); }
public ActionResult ChangeEmail(ChangeEmailViewModel model) { if (ModelState.IsValid) { //Update Account var context = HttpContext.GetOwinContext().Get <ApplicationDbContext>(); ApplicationUserManager _userManager = new ApplicationUserManager(new Microsoft.AspNet.Identity.EntityFramework.UserStore <ApplicationUser>(context)); var user = _userManager.FindByName(User.Identity.Name); var oldEmail = user.Email; user.Email = model.Email; _userManager.Update(user); HttpContext.GetOwinContext().Get <ApplicationDbContext>().SaveChanges(); //Update Newsletter using (var db = new SiteDbContext()) { var ifEmailExist = db.NewsletterRegistration .Any(r => r.Email.Equals(oldEmail)); if (ifEmailExist) { NewsletterRegistration newsletterRegistration = db.NewsletterRegistration .Where(r => r.Email == oldEmail) .Select(r => r).FirstOrDefault(); newsletterRegistration.Email = model.Email; db.Entry(newsletterRegistration).State = System.Data.Entity.EntityState.Modified; db.SaveChanges(); } } //Update YouTube Alert using (var db = new SiteDbContext()) { var ifEmailExist = db.YouTubeAlertRegistration .Any(r => r.Email.Equals(oldEmail)); if (ifEmailExist) { YouTubeAlertRegistration youTubeAlertRegistration = db.YouTubeAlertRegistration .Where(r => r.Email == oldEmail) .Select(r => r).FirstOrDefault(); youTubeAlertRegistration.Email = model.Email; db.Entry(youTubeAlertRegistration).State = System.Data.Entity.EntityState.Modified; db.SaveChanges(); } } return(RedirectToAction("Index", new { Message = "Adresse Email modifier avec succès !" })); } return(View(model)); }
public ActionResult Edit([Bind(Include = "Id,Username,Password")] User user) { if (ModelState.IsValid) { db.Entry(user).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); } return(View(user)); }
public ActionResult RedactionToDoListDelete(int id) { using (var context = new SiteDbContext()) { ItemsToDoList team = context.ItemsToDoList.Find(id); context.Entry(team).State = System.Data.Entity.EntityState.Deleted; context.SaveChanges(); } return(RedirectToAction("Redaction", "Admin")); }
public ActionResult UpdateConf(Conf model) { using (var context = new SiteDbContext()) { context.Entry(model).State = System.Data.Entity.EntityState.Modified; context.SaveChanges(); } Response.StatusCode = (int)System.Net.HttpStatusCode.OK; return(Json(new { success = true }, JsonRequestBehavior.AllowGet)); }
public ActionResult Edit(User user) { db.Configuration.ValidateOnSaveEnabled = false; db.Entry(user).State = EntityState.Modified; db.SaveChanges(); if (user.Admin) { return(RedirectToAction("Index")); } return(RedirectToAction("Details/" + user.UserID, "User")); }
public ActionResult RedactionToDoListAjaxUpdate(int id, int state) { using (var context = new SiteDbContext()) { ItemsToDoList item = context.ItemsToDoList.Find(id); item.State = state; context.Entry(item).State = System.Data.Entity.EntityState.Modified; context.SaveChanges(); } return(Json("", JsonRequestBehavior.AllowGet)); }
public ActionResult DebanUser(string userId) { HttpUtility.UrlDecode(userId); using (var context = new SiteDbContext()) { AspNetUserBan deban = context.AspNetUserBan.Find(userId); context.Entry(deban).State = System.Data.Entity.EntityState.Deleted; context.SaveChanges(); } return(Redirect(ControllerContext.HttpContext.Request.UrlReferrer.ToString())); }
public ActionResult DeleteComment(int id) { try { using (var context = new SiteDbContext()) { var model = context.Comments.Find(id); if (model.UserId == User.Identity.GetUserId() || User.IsInRole("administrator") || User.IsInRole("writer") || User.IsInRole("moderator")) { context.Entry(model).State = System.Data.Entity.EntityState.Deleted; context.SaveChanges(); } } Response.StatusCode = (int)System.Net.HttpStatusCode.OK; return(Json(new { success = true }, JsonRequestBehavior.AllowGet)); } catch { Response.StatusCode = (int)System.Net.HttpStatusCode.Forbidden; return(Json(new { success = false }, JsonRequestBehavior.AllowGet)); } }
public void UpdateTicket(string AssetId, string IssueAddress) { Ticket.AssetId = AssetId; Ticket.IssueAddress = IssueAddress; Ticket.Issued += Amount; SiteDbContext db = new SiteDbContext(); db.Entry<Entities.Tickets>(Ticket).State = System.Data.Entity.EntityState.Modified; db.SaveChanges(); }
public ActionResult Edit(Movie movie) { db.Entry(movie).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); }
public void Update(TEntity entity) { _context.Entry(entity).State = EntityState.Modified; _context.SaveChanges(); }
public ActionResult InLive(Conf model) { var modelConf = new Conf(); using (var context = new SiteDbContext()) { modelConf = context.Conf.Find(1); } using (var context = new SiteDbContext()) { if (modelConf.Value == "") { context.Entry(model).State = System.Data.Entity.EntityState.Modified; context.SaveChanges(); var checkBox = Request.Form["preventEmail"]; if (checkBox == "true") { using (var db = new SiteDbContext()) { var HeaderModel = context.Articles .OrderByDescending(r => r.Date) .Where(r => r.PublishState == 2) .Take(2) .Select(r => new ArticlesViewModel { Id = r.Id, Title = r.Title, Description = r.Description, Date = r.Date, Views = r.Views, PublishState = r.PublishState }).ToArray(); List <YouTubeAlertRegistration> newsletterModel = db.YouTubeAlertRegistration.ToList(); newsletterModel.ForEach(delegate(YouTubeAlertRegistration user) //Send for each user { dynamic email = new Email("YouTubeAlertEmail"); email.To = user.Email; email.Title = "[Gamers Addict] " + model.Name; email.Text = "J\'ai commencé une diffusion en direct du nom de " + model.Name + " sur YouTube: <a href=\"https://www.youtube.com/watch?v=" + model.Value + "\">https://www.youtube.com/watch?v=" + model.Value + "</a>"; email.Email = user.Email; email.SecretKey = user.SecretKey; email.ArticleOneId = HeaderModel[0].Id; email.ArticleOneTitle = HeaderModel[0].Title; email.ArticleOneDescription = HeaderModel[0].Description; email.ArticleTwoId = HeaderModel[1].Id; email.ArticleTwoTitle = HeaderModel[1].Title; email.ArticleTwoDescription = HeaderModel[1].Description; email.Send(); }); } } return(Json("Début du live !", JsonRequestBehavior.AllowGet)); } else { model.Name = ""; model.Value = ""; context.Entry(model).State = System.Data.Entity.EntityState.Modified; context.SaveChanges(); return(Json("Fin du live !", JsonRequestBehavior.AllowGet)); } } }
public ActionResult Post(Articles model, HttpPostedFileBase file) { if (!ModelState.IsValid) { ModelState.AddModelError("", "Erreur dans le formulaire"); return(View(model)); } model.Tags.Trim(); // Replace Space using (var context = new SiteDbContext()) { if (model.Id == null) //Add { model.AuthorId = User.Identity.GetUserId(); model.Date = DateTime.Now; context.Articles.Add(model); UpdateSitemapXML(); } else //Update { model.EditDate = DateTime.Now; context.Entry(model).State = System.Data.Entity.EntityState.Modified; } context.SaveChanges(); } //Upload Image if (file != null && file.ContentLength > 0 && System.IO.Path.GetExtension(file.FileName) == ".jpg") { string path = System.IO.Path.Combine(Server.MapPath("~/Images/Uploads/Articles/Header/"), model.Id.ToString()); WebImage img = new WebImage(file.InputStream); //Resize image if (img.Width >= 1280 || img.Height >= 720) { if (System.IO.File.Exists(path + "x1280")) { System.IO.File.Delete(path + "x1280"); } img.Resize(1280, 720); img.Save(path + "x1280"); if (System.IO.File.Exists(path + "x800")) { System.IO.File.Delete(path + "x800"); } img.Resize(800, 450); img.Save(path + "x800"); if (System.IO.File.Exists(path + "x320")) { System.IO.File.Delete(path + "x320"); } img.Resize(320, 180); img.Save(path + "x320"); } else { ModelState.AddModelError("", "Erreur, l'image n'a pas été modifier, résolution invalide !"); return(View(model)); } } return(RedirectToAction("Articles", "Admin")); }