public void Add(ViewModels.Category category) { var cate = new DataModels.Category() { CategoryName = category.CategoryName, Description = category.Description, Picture = category.Image }; dbContext.Categories.Add(cate); dbContext.SaveChanges(); }
public async Task <ServiceModels.ServiceResponse> UpdateBoard(InputModels.EditBoardInput input) { var serviceResponse = new ServiceModels.ServiceResponse(); var record = (await Records()).FirstOrDefault(b => b.Id == input.Id); if (record is null) { serviceResponse.Error($"A record does not exist with ID '{input.Id}'"); } DataModels.Category newCategoryRecord = null; if (!string.IsNullOrEmpty(input.NewCategory)) { input.NewCategory = input.NewCategory.Trim(); } if (!string.IsNullOrEmpty(input.NewCategory)) { newCategoryRecord = (await Categories()).FirstOrDefault(c => c.Name == input.NewCategory); if (newCategoryRecord is null) { var displayOrder = (await Categories()).DefaultIfEmpty().Max(c => c.DisplayOrder); newCategoryRecord = new DataModels.Category { Name = input.NewCategory, DisplayOrder = displayOrder + 1 }; DbContext.Categories.Add(newCategoryRecord); DbContext.SaveChanges(); } } else { try { var newCategoryId = Convert.ToInt32(input.Category); newCategoryRecord = (await Categories()).FirstOrDefault(c => c.Id == newCategoryId); if (newCategoryRecord is null) { serviceResponse.Error(nameof(input.Category), "No category was found with this ID."); } } catch (FormatException) { serviceResponse.Error(nameof(input.Category), "Invalid category ID"); } } if (!string.IsNullOrEmpty(input.Name)) { input.Name = input.Name.Trim(); } if (string.IsNullOrEmpty(input.Name)) { serviceResponse.Error(nameof(input.Name), "Name is a required field."); } if (!string.IsNullOrEmpty(input.Description)) { input.Description = input.Description.Trim(); } if (!serviceResponse.Success) { return(serviceResponse); } record.Name = input.Name; record.Description = input.Description; var oldCategoryId = -1; if (record.CategoryId != newCategoryRecord.Id) { var categoryBoards = (await Records()).Where(r => r.CategoryId == record.CategoryId).ToList(); if (categoryBoards.Count() <= 1) { oldCategoryId = record.CategoryId; } record.CategoryId = newCategoryRecord.Id; } var boardRoles = (from role in await RoleRepository.BoardRoles() where role.BoardId == record.Id select role).ToList(); foreach (var boardRole in boardRoles) { DbContext.BoardRoles.Remove(boardRole); } if (input.Roles != null) { var roleIds = (from role in await RoleRepository.SiteRoles() select role.Id).ToList(); foreach (var inputRole in input.Roles) { if (roleIds.Contains(inputRole)) { DbContext.BoardRoles.Add(new DataModels.BoardRole { BoardId = record.Id, RoleId = inputRole }); } else { serviceResponse.Error($"Role does not exist with id '{inputRole}'"); } } } if (!serviceResponse.Success) { return(serviceResponse); } DbContext.Update(record); DbContext.SaveChanges(); if (oldCategoryId >= 0) { var oldCategoryRecord = (await Categories()).FirstOrDefault(item => item.Id == oldCategoryId); if (oldCategoryRecord != null) { DbContext.Categories.Remove(oldCategoryRecord); DbContext.SaveChanges(); } } serviceResponse.RedirectPath = UrlHelper.Action(nameof(Controllers.Boards.Manage), nameof(Controllers.Boards), new { id = record.Id }); return(serviceResponse); }
public async Task <ServiceModels.ServiceResponse> AddBoard(InputModels.CreateBoardInput input) { var serviceResponse = new ServiceModels.ServiceResponse(); if ((await Records()).Any(b => b.Name == input.Name)) { serviceResponse.Error(nameof(input.Name), "A board with that name already exists"); } DataModels.Category categoryRecord = null; if (!string.IsNullOrEmpty(input.NewCategory)) { input.NewCategory = input.NewCategory.Trim(); } if (!string.IsNullOrEmpty(input.NewCategory)) { categoryRecord = (await Categories()).FirstOrDefault(c => c.Name == input.NewCategory); if (categoryRecord is null) { var displayOrder = (await Categories()).DefaultIfEmpty().Max(c => c.DisplayOrder); categoryRecord = new DataModels.Category { Name = input.NewCategory, DisplayOrder = displayOrder + 1 }; DbContext.Categories.Add(categoryRecord); } } else { try { var categoryId = Convert.ToInt32(input.Category); categoryRecord = (await Categories()).First(c => c.Id == categoryId); if (categoryRecord is null) { serviceResponse.Error(nameof(input.Category), "No category was found with this ID."); } } catch (FormatException) { serviceResponse.Error(nameof(input.Category), "Invalid category ID"); } } if (!string.IsNullOrEmpty(input.Name)) { input.Name = input.Name.Trim(); } if (string.IsNullOrEmpty(input.Name)) { serviceResponse.Error(nameof(input.Name), "Name is a required field."); } if (!string.IsNullOrEmpty(input.Description)) { input.Description = input.Description.Trim(); } var existingRecord = (await Records()).FirstOrDefault(b => b.Name == input.Name); if (existingRecord != null) { serviceResponse.Error(nameof(input.Name), "A board with that name already exists"); } if (!serviceResponse.Success) { return(serviceResponse); } DbContext.SaveChanges(); var record = new DataModels.Board { Name = input.Name, Description = input.Description, CategoryId = categoryRecord.Id }; DbContext.Boards.Add(record); DbContext.SaveChanges(); serviceResponse.RedirectPath = UrlHelper.Action(nameof(Controllers.Boards.Manage), nameof(Controllers.Boards), new { id = record.Id }); return(serviceResponse); }