public static void DeleteSynonymForString(CMSDatabase db, int?itemID, HttpContext context, out bool successfullyCompleted) { if (itemID.HasValue) { SynonymForString synonymForString = db.SynonymsForStrings.FirstOrDefault(s => s.ID == itemID); if (synonymForString == null) { successfullyCompleted = false; return; } db.Remove(synonymForString); db.SaveChanges(); successfullyCompleted = true; LogManagementFunctions.AddAdminPanelLog( db: db, context: context, info: $"{synonymForString.String} -> {synonymForString.Synonym}: {(context.Items["LogLocalization"] as IAdminPanelLogLocalization)?.SynonymForStringDeleted}" ); } else { successfullyCompleted = false; } }
public static void DeleteUser(CMSDatabase db, int?userID, HttpContext context, out int statusCode) { if (!userID.HasValue || userID.Value == 1) { statusCode = 404; return; } User user = db.Users.FirstOrDefault(u => u.ID == userID.Value); if (user == null) { statusCode = 404; return; } if (user == context.Items["User"]) { context.Items["User"] = null; } db.Remove(user); db.SaveChanges(); statusCode = 200; LogManagementFunctions.AddAdminPanelLog( db: db, context: context, info: $"{user.Login} (ID-{user.ID.ToString()}): {(context.Items["LogLocalization"] as IAdminPanelLogLocalization)?.UserDeleted}" ); }
public static void DeleteRedirection(CMSDatabase db, int?itemID, HttpContext context, out bool successfullyCompleted) { if (itemID.HasValue) { Redirection redirection = db.Redirections.FirstOrDefault(r => r.ID == itemID); if (redirection == null) { successfullyCompleted = false; return; } db.Remove(redirection); db.SaveChanges(); successfullyCompleted = true; LogManagementFunctions.AddAdminPanelLog( db: db, context: context, info: $"{redirection.RequestPath} -> {redirection.RedirectionPath}: {(context.Items["LogLocalization"] as IAdminPanelLogLocalization)?.RedirectionDeleted}" ); } else { successfullyCompleted = false; } }
public static void RenameImageAndDependencies(CMSDatabase db, IHostingEnvironment env, string pathToImages, string oldImageName, string newImageName, string imageExtension, bool saveChangesInDB = true) { if (string.IsNullOrEmpty(pathToImages) || string.IsNullOrEmpty(oldImageName) || string.IsNullOrEmpty(newImageName) || !Directory.Exists(pathToImages)) { return; } if (!pathToImages[pathToImages.Length - 1].Equals('/')) { pathToImages = pathToImages.Insert(pathToImages.Length, "/"); } string[] images = Directory.GetFiles(pathToImages, $"*{oldImageName}*{imageExtension}"); Regex regex = new Regex($"{oldImageName}(_\\d+x\\d+)?(_q\\d{{1,3}})?{imageExtension}$"); images = (from img in images where regex.IsMatch(img) select img).ToArray(); foreach (var img in images) { string fileEnding = img.Substring(pathToImages.Length + oldImageName.Length); File.Move(img, $"{pathToImages}{newImageName}{fileEnding}"); } string shortPathToOldImage = pathToImages.Substring(env.GetStorageFolderFullPath().Length).Insert(0, "/") + oldImageName + imageExtension; string shortPathToNewImage = pathToImages.Substring(env.GetStorageFolderFullPath().Length).Insert(0, "/") + newImageName + imageExtension; // Изменяем данные в БД // Если в БД есть неудаленная информация, то удаляем её Image image = db.Images.FirstOrDefault(img => img.ShortPathHash == OtherFunctions.GetHashFromString(shortPathToNewImage) && img.ShortPath.Equals(shortPathToNewImage, StringComparison.Ordinal)); if (image != null && db.Entry(image).State != EntityState.Modified) { db.Remove(image); } image = db.Images.FirstOrDefault(img => img.ShortPathHash == OtherFunctions.GetHashFromString(shortPathToOldImage) && img.ShortPath.Equals(shortPathToOldImage, StringComparison.Ordinal)); if (image != null) { if (db.Entry(image).State == EntityState.Deleted) { db.Entry(image).State = EntityState.Modified; } image.ShortPath = shortPathToNewImage; image.ShortPathHash = OtherFunctions.GetHashFromString(shortPathToNewImage); image.FullName = shortPathToNewImage.Substring(shortPathToNewImage.LastIndexOf('/') + 1); } if (saveChangesInDB) { db.SaveChanges(); } }
public static void DeletePage(CMSDatabase db, PageType?pageType, int?itemID, HttpContext context, out bool successfullyDeleted) { if (!pageType.HasValue || !itemID.HasValue) { successfullyDeleted = false; return; } Page page = null; switch (pageType) { case PageType.Usual: page = db.UsualPages.FirstOrDefault(p => p.ID == itemID); break; case PageType.Category: page = db.CategoryPages.FirstOrDefault(p => p.ID == itemID); break; default: successfullyDeleted = false; return; } if (page == null) { successfullyDeleted = false; return; } if (page is UsualPage up) { // Получаем все зависимые страницы List <UsualPage> usualPages = db.UsualPages.Where(p => p.PreviousPageID == up.ID).ToList(); List <CategoryPage> categoryPages = db.CategoryPages.Where(p => p.PreviousPageID == up.ID).ToList(); db.UsualPages.Remove(up); db.SaveChanges(); // Обновляем полученные зависимые страницы foreach (var u_page in usualPages) { u_page.PreviousPageID = up.PreviousPageID; RefreshPageAndDependencies(db, u_page); } foreach (var c_page in categoryPages) { c_page.PreviousPageID = up.PreviousPageID; RefreshPageAndDependencies(db, c_page); } } else if (page is CategoryPage cp) { IHostingEnvironment env = context.RequestServices.GetRequiredService <IHostingEnvironment>(); cp.ProductPages = db.ProductPages.Where(pp => pp.PreviousPageID == cp.ID).ToList(); foreach (var p in cp.ProductPages) { string[] images = ImagesManagementFunctions.GetProductImageUrls(p, env); for (int i = 0; i < images.Length; ++i) { Image image = db.Images.FirstOrDefault(img => img.ShortPathHash == OtherFunctions.GetHashFromString(images[i]) && img.ShortPath.Equals(images[i], StringComparison.Ordinal)); if (image != null) { db.Images.Remove(image); } } string pathToImages = $"{env.GetProductsImagesFolderFullPath()}{p.ID}/"; if (Directory.Exists(pathToImages)) { Directory.Delete(pathToImages, true); } } db.Remove(page); } db.SaveChanges(); successfullyDeleted = true; LogManagementFunctions.AddAdminPanelLog( db: db, context: context, info: $"{page.PageName} (ID-{page.ID.ToString()}): " + (page is UsualPage ? (context.Items["LogLocalization"] as IAdminPanelLogLocalization)?.PageDeleted : (context.Items["LogLocalization"] as IAdminPanelLogLocalization)?.CategoryDeleted) ); }