public async Task <IActionResult> Edit(long?id) { if (id == null) { return(NotFound()); } var componentType = await _context.ComponentType.FindAsync(id); if (componentType == null) { return(NotFound()); } var componentTypeViewModel = new ComponentTypeViewModel() { ComponentTypeId = componentType.ComponentTypeId, AdminComment = componentType.AdminComment, ComponentInfo = componentType.ComponentInfo, ComponentName = componentType.ComponentName, Datasheet = componentType.Datasheet, ImageUrl = componentType.ImageUrl, Location = componentType.Location, Manufacturer = componentType.Manufacturer, Status = componentType.Status, WikiLink = componentType.WikiLink, Image = new FormFile(Stream.Null, 0, 0, componentType.FileName, componentType.FileName) }; return(View(componentTypeViewModel)); }
// GET public IActionResult Index() { var vm = new ComponentTypeViewModel(); vm.AllComponentTypes = _componentTypeRepository.GetAllComponentTypes(); return(View("Index", vm)); }
public async Task <IActionResult> Create(ComponentTypeViewModel componentTypeVM) { if (!ModelState.IsValid) { return(View(componentTypeVM)); } if (componentTypeVM.SelectedCategories != null) { var selectedCategories = _context.Categories .Where(x => componentTypeVM.SelectedCategories .Contains(x.CategoryId.ToString())); var cctList = new List <CategoryComponentType>(); foreach (var selectedCategory in selectedCategories) { var cct = new CategoryComponentType { CategoryId = selectedCategory.CategoryId, ComponentType = componentTypeVM.ComponentType }; cctList.Add(cct); } componentTypeVM.ComponentType.CategoryComponentTypes = cctList; } _context.Add(componentTypeVM.ComponentType); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); }
public ActionResult EditType(ComponentTypeViewModel model) { var updatedEntity = AutoMapper.Mapper.Map <ComponentType>(model); _typeRepo.Update(updatedEntity); return(RedirectToAction("ShowTypes")); }
public ActionResult AddComponentType(ComponentTypeViewModel model) { var newEntity = AutoMapper.Mapper.Map <ComponentType>(model); _typeRepo.Insert(newEntity); return(RedirectToAction("ShowTypes")); }
// GET: ComponentType public IActionResult Index() { var componentTypeVMList = new List <ComponentTypeViewModel>(); foreach (var componentType in _context.ComponentTypes.ToList()) { var categoryIds = _context.CategoryComponentType.Where(x => x.ComponentTypeId == componentType.ComponentTypeId).Select(i => i.CategoryId).ToList(); var categories = new List <string>(); foreach (var categoryId in categoryIds) { categories.Add(_context.Categories.Where(x => x.CategoryId == categoryId).Select(x => x.Name).FirstOrDefault()); } var componentTypeVM = new ComponentTypeViewModel() { ComponentType = componentType, SelectedCategories = categories }; componentTypeVMList.Add(componentTypeVM); } return(View(componentTypeVMList)); }
public async Task <IActionResult> Put([FromRoute] int id, [FromBody] ComponentTypeViewModel componentType) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != componentType.ComponentTypeId) { return(BadRequest()); } try { await _repository.UpdateComponentType(componentType); return(CreatedAtAction("GetEntityType", new { id = componentType.ComponentTypeId }, componentType)); } catch (DbUpdateConcurrencyException) { if (!ComponentTypeExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public ViewResult Create() { SetupViewBagsForComponentTypeAndCategory(); var model = new ComponentTypeViewModel(); return(View(model)); }
public ViewResult Edit(long id) { SetupViewBagsForComponentTypeAndCategory(); var data = _context.ComponentTypes.Find(id); var componentTypeViewModel = ComponentTypeViewModel.ParseToComponentViewModel(data, _context.Categories.ToList()); return(View(componentTypeViewModel)); }
public ActionResult Create(ComponentTypeViewModel data) { ComponentType componentType = GetComponentType(data); _context.Add(componentType); _context.SaveChanges(); return(RedirectToAction(nameof(Index), "ComponentType")); }
private ComponentType GetComponentType(ComponentTypeViewModel data) { var componentType = ComponentType.ParseToComponent(data, null); List <Category> categories = GetCategoryList(data.CategoryIds); List <CategoryToComponentType> categoryToComponentTypes = GetCategoryToComponentList(componentType, categories); componentType.CategoryToComponentTypes = categoryToComponentTypes; return(componentType); }
public ActionResult AddComponentType() { var result = new ComponentTypeViewModel { AvailableCategories = AutoMapper.Mapper.Map <List <ComponentCategoryViewModel> >(_categoryRepo.Get()) }; return(View(result)); }
public IActionResult Edit(ComponentTypeViewModel componentTypeVM) { if (!ModelState.IsValid) { return(View(componentTypeVM)); } var id = componentTypeVM.ComponentType.ComponentTypeId; try { _context.CategoryComponentType.RemoveRange( _context.CategoryComponentType.Where(x => x.ComponentTypeId == id)); if (componentTypeVM.SelectedCategories != null) { var selectedCategories = _context.Categories .Where(x => componentTypeVM.SelectedCategories .Contains(x.CategoryId.ToString())); var cctList = new List <CategoryComponentType>(); foreach (var selectedCategory in selectedCategories) { var cct = new CategoryComponentType { CategoryId = selectedCategory.CategoryId, ComponentType = componentTypeVM.ComponentType }; cctList.Add(cct); } componentTypeVM.ComponentType.CategoryComponentTypes = cctList; } _context.Update(componentTypeVM.ComponentType); _context.SaveChanges(); } catch (DbUpdateConcurrencyException) { if (!ComponentTypeExists(id)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); }
public async Task UpdateComponentType(ComponentTypeViewModel componentTypes) { var c = new ComponentType { ComponentTypeId = componentTypes.ComponentTypeId, Name = componentTypes.Name, IsActive = componentTypes.IsActive }; _db.ComponentTypes.Update(c); await _db.SaveChangesAsync(); }
// GET: ComponentType/Create public IActionResult Create() { var componentTypeVM = new ComponentTypeViewModel(); componentTypeVM.Categories = _context.Categories.ToList().Select( cat => new SelectListItem { Text = cat.Name, Value = cat.CategoryId.ToString() }).ToList(); return(View(componentTypeVM)); }
public ActionResult Edit(int id, ComponentTypeViewModel data) { var test = _context.ComponentTypes.First(x => x.ComponentTypeId == id); data.ComponentTypeId = id; var componentType = GetComponentType(data); ComponentType.OverWrite(test, componentType); _context.ComponentTypes.Update(test); _context.SaveChanges(); return(RedirectToAction(nameof(Index), "ComponentType")); }
public async Task Put_IdNotMatch_ReturnBadRequest() { var firstManufacturer = _componentType[0]; var viewModel = new ComponentTypeViewModel { ComponentTypeId = 15, Name = firstManufacturer.Name, IsActive = firstManufacturer.IsActive }; viewModel.Name = "Kyllingsalat"; var result = await _controller.Put(_componentType[0].ComponentTypeId, viewModel); Assert.IsNotNull(result); Assert.IsTrue(result.GetType() == typeof(BadRequestResult)); }
public async Task Put_ModelIsNotValid_returnBadRequest() { var viewModelToPut = new ComponentTypeViewModel { Name = "Ikke Gimbal", IsActive = false }; _controller.ModelState.AddModelError("Put fail", "Put fail"); var result = await _controller.Put(viewModelToPut.ComponentTypeId, viewModelToPut); Assert.IsNotNull(result); Assert.IsTrue(result.GetType() == typeof(BadRequestObjectResult)); }
public IActionResult Create() { var categoriesList = new List <SelectListItem>(); var viewModel = new ComponentTypeViewModel() { Categories = _context.Category.ToList().Select(x => new SelectListItem() { Text = x.Name, Value = x.CategoryId.ToString() }) }; return(View(viewModel)); }
// GET: ComponentType/Create public async Task <IActionResult> Create() { var categoriesAsSelectList = await _context.Category.Select(c => new SelectListItem { Text = c.Name, Value = c.CategoryId.ToString() }).ToListAsync(); ComponentTypeViewModel vm = new ComponentTypeViewModel { MultiSelectCategories = new MultiSelectList(categoriesAsSelectList.OrderBy(c => c.Text), "Value", "Text"), }; return(View(vm)); }
public async Task Put_PutComponentType_ReturnCreatedAtActionResult() { _mockRepositoryComponent.Setup(x => x.GetComponentType(It.Is <int>(i => i == 1))) .ReturnsAsync(_componentType.First(x => x.ComponentTypeId == 1)); var viewModelToPut = new ComponentTypeViewModel { Name = "Ikke Gimbal", IsActive = false }; var result = await _controller.Put(viewModelToPut.ComponentTypeId, viewModelToPut); Assert.IsNotNull(result); Assert.IsTrue(result.GetType() == typeof(CreatedAtActionResult)); Assert.AreEqual(viewModelToPut, (result as CreatedAtActionResult)?.Value); }
public async Task <IActionResult> Create([Bind("SelectedCategories, ComponentName,ComponentInfo,Location,Status,Datasheet,ImageUrl,Manufacturer,WikiLink,AdminComment")] ComponentTypeViewModel vm) { if (ModelState.IsValid) { ComponentType tempComponentType = new ComponentType() { ComponentName = vm.ComponentName, AdminComment = vm.AdminComment, ComponentInfo = vm.ComponentInfo, Datasheet = vm.Datasheet, Location = vm.Location, WikiLink = vm.WikiLink, Status = vm.Status, Manufacturer = vm.Manufacturer, ImageUrl = vm.ImageUrl, Image = vm.Image, }; try { var componentType = _context.Add(tempComponentType).Entity; foreach (var id in vm.SelectedCategories) { Category cat = _context.Category.Find(long.Parse(id)); if (cat != null) { ComponentTypeCategory ctc = new ComponentTypeCategory { Category = cat, ComponentType = componentType, }; _context.Add(ctc); } } await _context.SaveChangesAsync(); } catch (Exception ex) { throw ex; } return(RedirectToAction(nameof(Index))); } return(View(vm)); }
public async Task <IActionResult> Create(ComponentTypeViewModel componentType) { if (ModelState.IsValid) { var componentTypeModel = new ComponentType() { ComponentName = componentType.ComponentName, ComponentInfo = componentType.ComponentInfo, Status = componentType.Status, Datasheet = componentType.Datasheet, WikiLink = componentType.WikiLink, Manufacturer = componentType.Manufacturer, AdminComment = componentType.AdminComment, ImageUrl = componentType.ImageUrl, Location = componentType.Location, CategorieIdsList = componentType.SelectedCategories }; if (!componentType.Image.FileName.EndsWith(".jpg") && !componentType.Image.FileName.EndsWith(".png")) { ViewBag.ImgError = "Please Insert an image of the right file type"; return(View("Edit", componentType)); } if (componentType.Image != null) { using (var ms = new MemoryStream()) { await componentType.Image.CopyToAsync(ms); var fileBytes = ms.ToArray(); componentTypeModel.Image = fileBytes; componentTypeModel.ImageMimeType = componentType.Image.ContentType; componentTypeModel.FileName = componentType.Image.FileName; } } _context.Add(componentTypeModel); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(ComponentTypesIndex))); } return(View(componentType)); }
public IActionResult ComponentTypes(int id) { //var category = CategoryMock.GetCategories().SingleOrDefault(c => c.CategoryId == id); //var componentTypes = category.CategoryComponentTypes.Select(categoryComponentType => categoryComponentType.ComponentType).ToList(); //var category = _unitOfWork.Catagories.Include(c => c.CategoryComponentTypes).ThenInclude(comp => comp.ComponentType).Single(c => c.CategoryId == id); var category = _unitOfWork.Categories.GetCategoryWithComponentTypes(id); //TODO correct way or make async and get from db ?? var componentTypes = category.CategoryComponentTypes.Select(categoryComponenType => categoryComponenType.ComponentType).ToList(); var viewModel = new ComponentTypeViewModel { Category = category, ComponentTypes = componentTypes }; return(View(viewModel)); }
public IActionResult SaveComponentType(ComponentTypeViewModel viewModel) { //TODO: Create new ComponentType in Database if (viewModel.ComponentType.ComponentTypeId == 0) { //If creating viewModel.ComponentType.Status = ComponentTypeStatus.ReservedAdmin; var categoryComponentType = new CategoryComponentType(); categoryComponentType.CategoryId = viewModel.Category.CategoryId; categoryComponentType.ComponentType = viewModel.ComponentType; _unitOfWork.CategoryComponentTypes.Add(categoryComponentType); } else { //If editing var componentTypeInDb = _unitOfWork.ComponentTypes.Get(viewModel.ComponentType.ComponentTypeId); componentTypeInDb.ComponentName = viewModel.ComponentType.ComponentName; componentTypeInDb.ComponentInfo = viewModel.ComponentType.ComponentInfo; componentTypeInDb.Location = viewModel.ComponentType.Location; componentTypeInDb.Status = viewModel.ComponentType.Status; componentTypeInDb.Datasheet = viewModel.ComponentType.Datasheet; componentTypeInDb.ImageUrl = viewModel.ComponentType.ImageUrl; componentTypeInDb.Manufacturer = viewModel.ComponentType.Manufacturer; componentTypeInDb.WikiLink = viewModel.ComponentType.WikiLink; componentTypeInDb.AdminComment = viewModel.ComponentType.AdminComment; componentTypeInDb.Image = viewModel.ComponentType.Image; } _unitOfWork.Complete(); return(RedirectToAction("ComponentTypes", "ComponentType", new { id = viewModel.Category.CategoryId })); }
// GET: ComponentType/Edit/5 public async Task <IActionResult> Edit(long?id) { if (id == null) { return(NotFound()); } var componentType = await _context.ComponentTypes .SingleOrDefaultAsync(m => m.ComponentTypeId == id); if (componentType == null) { return(NotFound()); } var categoryIds = _context.CategoryComponentType.Where(x => x.ComponentTypeId == componentType.ComponentTypeId).Select(i => i.CategoryId).ToList(); var categories = new List <string>(); foreach (var categoryId in categoryIds) { var category = _context.Categories.Where(x => x.CategoryId == id).Select(x => x.Name).FirstOrDefault(); if (category != null) { categories.Add(category); } } var componentTypeVM = new ComponentTypeViewModel() { ComponentType = componentType, SelectedCategories = categories, Categories = _context.Categories.ToList().Select( cat => new SelectListItem { Text = cat.Name, Value = cat.CategoryId.ToString() }).ToList() }; return(View(componentTypeVM)); }
public async Task <IActionResult> Edit([FromBody] ComponentTypeViewModel componentType) { if (ModelState.IsValid) { var componentTypeModel = new ComponentType() { ComponentName = componentType.ComponentName, ComponentInfo = componentType.ComponentInfo, Status = componentType.Status, Datasheet = componentType.Datasheet, WikiLink = componentType.WikiLink, Manufacturer = componentType.Manufacturer, AdminComment = componentType.AdminComment, ImageUrl = componentType.ImageUrl, }; if (!componentType.Image.FileName.EndsWith(".jpg")) { ViewBag.ImgError = "Please Insert an image of the right file type"; return(View("Edit", componentType)); } if (componentType.Image != null) { using (var reader = new StreamReader(componentType.Image.OpenReadStream())) { string contentAsString = reader.ReadToEnd(); byte[] contentAsByteArray = GetBytes(contentAsString); componentTypeModel.Image = contentAsByteArray; componentTypeModel.ImageMimeType = componentType.Image.ContentType; componentTypeModel.FileName = componentType.Image.FileName; } } _context.Update(componentTypeModel); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(ComponentTypesIndex))); } return(View(componentType)); }
// GET: ComponentType/Edit/5 public async Task <IActionResult> Edit(long?id) { if (id == null) { return(NotFound()); } var componentType = await _context.ComponentType.FindAsync(id); if (componentType == null) { return(NotFound()); } var categoriesAsSelectList = await _context.Category.Select(c => new SelectListItem { Text = c.Name, Value = c.CategoryId.ToString() }).ToListAsync(); ComponentTypeViewModel vm = new ComponentTypeViewModel { ComponentName = componentType.ComponentName, AdminComment = componentType.AdminComment, ComponentInfo = componentType.ComponentInfo, Datasheet = componentType.Datasheet, Location = componentType.Location, WikiLink = componentType.WikiLink, Status = componentType.Status, Manufacturer = componentType.Manufacturer, ImageUrl = componentType.ImageUrl, Image = componentType.Image, MultiSelectCategories = new MultiSelectList(categoriesAsSelectList.OrderBy(c => c.Text), "Value", "Text"), ComponentTypeId = componentType.ComponentTypeId, }; return(View(vm)); }
public IActionResult SearchComponentType(ComponentTypeViewModel viewModel) { var category = _unitOfWork.Categories.GetCategoryWithComponentTypes(viewModel.Category.CategoryId); IEnumerable <CategoryComponentType> searchResult; if (!string.IsNullOrWhiteSpace(viewModel.SearchText)) { searchResult = from cType in category.CategoryComponentTypes where cType.ComponentType.ComponentName.ToLower().Contains(viewModel.SearchText.ToLower()) || cType.ComponentType.WikiLink.ToLower().Contains(viewModel.SearchText.ToLower()) || cType.ComponentType.Status.ToString().ToLower().Contains(viewModel.SearchText.ToLower()) select cType; } else { searchResult = from cType in category.CategoryComponentTypes select cType; } var componentTypes = searchResult.Select(categoryComponenType => categoryComponenType.ComponentType).ToList(); var returnViewModel = new ComponentTypeViewModel { Category = category, ComponentTypes = componentTypes }; return(View(returnViewModel)); }
public ComponentTypeViewModel GetComponentTypeViewModel(int?id) { ComponentTypeViewModel cViewModel; if (id == null) { cViewModel = new ComponentTypeViewModel(); } else { cViewModel = (from c in _db.ComponentTypes where c.ComponentTypeId == id select new ComponentTypeViewModel() { ComponentTypeId = c.ComponentTypeId, Name = c.Name, IsActive = c.IsActive } ).FirstOrDefault(); } return(cViewModel); }