public MenuItemCategory(MenuItemCategoryEntity menuItemCategory) : base(menuItemCategory) { Name = menuItemCategory.Name; Description = menuItemCategory.Description; }
/// <summary> /// Rejestruje w systemie kategorię elementów menu. /// </summary> /// <param name="managerId">Identyfikator użytkownika o roli menedżera baru.</param> /// <param name="name">Nazwa kategorii</param> /// <param name="description">Opis kategorii</param> /// <returns></returns> public MenuItemCategory AddMenuItemCategory(int managerId, string name, string description) { if (!CheckHasUserRole(managerId, UserRole.Manager)) throw new SecurityException(String.Format("User id = {0} is not logged in or is no manager", managerId)); if (String.IsNullOrEmpty(name)) throw new ArgumentNullException("Name is null"); if (String.IsNullOrEmpty(description)) throw new ArgumentNullException("Description is null"); MenuItemCategoryEntity newCategoryEntity = null; using( var db = new DataAccessProvider()) { var categoryToAddEntity = new MenuItemCategoryEntity() {Name = name, Description = description}; var categoriesSameName = db.MenuItemCategories.Where(c => c.Name.Equals(name)); if(categoriesSameName != null && categoriesSameName.Any()) foreach(MenuItemCategoryEntity category in categoriesSameName) if(category.Equals(categoryToAddEntity)) { if(category.IsDeleted) category.IsDeleted = false; newCategoryEntity = category; break; } if(newCategoryEntity == null) newCategoryEntity = db.MenuItemCategories.Add(categoryToAddEntity); db.SaveChanges(); } return new MenuItemCategory(newCategoryEntity); }