public async Task <IActionResult> Edit(int id, [Bind("Id,Name,Info,Cost,Year,FormId,MaterialId,TypeId,BrandId")] Guitars guitars) { if (id != guitars.Id) { return(NotFound()); } if (ModelState.IsValid) { try { _context.Update(guitars); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!GuitarsExists(guitars.Id)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } ViewData["BrandId"] = new SelectList(_context.Brands, "Id", "Name", guitars.BrandId); ViewData["FormId"] = new SelectList(_context.Forms, "Id", "Name", guitars.FormId); ViewData["MaterialId"] = new SelectList(_context.Materials, "Id", "Name", guitars.MaterialId); ViewData["TypeId"] = new SelectList(_context.Types, "Id", "Name", guitars.TypeId); return(View(guitars)); }
public async Task <IActionResult> Create([Bind("Id,Name,Info,Cost,Year,FormId,MaterialId,TypeId,BrandId")] Guitars guitars) { if (ModelState.IsValid) { _context.Add(guitars); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } ViewData["BrandId"] = new SelectList(_context.Brands, "Id", "Name", guitars.BrandId); ViewData["FormId"] = new SelectList(_context.Forms, "Id", "Name", guitars.FormId); ViewData["MaterialId"] = new SelectList(_context.Materials, "Id", "Name", guitars.MaterialId); ViewData["TypeId"] = new SelectList(_context.Types, "Id", "Name", guitars.TypeId); return(View(guitars)); }
public async Task <IActionResult> Import(IFormFile fileExcel) { if (ModelState.IsValid) { if (fileExcel != null) { List <Brands> addedBrands = new List <Brands>(); List <Types> addedTypes = new List <Types>(); List <Materials> addedMaterials = new List <Materials>(); List <Forms> addedForms = new List <Forms>(); List <Guitars> addedGuitars = new List <Guitars>(); using (var stream = new FileStream(fileExcel.FileName, FileMode.Create)) { await fileExcel.CopyToAsync(stream); using (XLWorkbook workBook = new XLWorkbook(stream, XLEventTracking.Disabled)) { //перегляд усіх листів (в даному випадку категорій) foreach (IXLWorksheet worksheet in workBook.Worksheets) { //worksheet.Name - назва категорії. Пробуємо знайти в БД, якщо відсутня, то створюємо нову Brands newbrand; var b = (from br in _context.Brands where br.Name.Contains(worksheet.Name) select br).ToList(); if (b.Count > 0) { newbrand = b[0]; } else { newbrand = new Brands(); newbrand.Name = worksheet.Name; newbrand.Country = "Ukraine"; addedBrands.Add(newbrand); //додати в контекст _context.Brands.Add(newbrand); } //перегляд усіх рядків foreach (IXLRow row in worksheet.RowsUsed()) { try { Guitars guitar = new Guitars(); var guitars = (from g in _context.Guitars where g.Name.Contains(row.Cell(1).Value.ToString()) select g).ToList(); if (guitars.Count > 0) { continue; } else { guitar.Name = row.Cell(1).Value.ToString(); guitar.Brand = newbrand; guitar.Cost = Convert.ToInt32(row.Cell(2).Value); guitar.Year = Convert.ToInt32(row.Cell(3).Value); guitar.Info = "from excel"; //у разі наявності автора знайти його, у разі відсутності - додати Forms form; var forms = (from f in _context.Forms where f.Name.Contains(row.Cell(4).Value.ToString()) select f).ToList(); if (forms.Count > 0) { form = forms[0]; } else { form = new Forms(); form.Name = row.Cell(4).Value.ToString(); addedForms.Add(form); _context.Forms.Add(form); } guitar.Form = form; Materials material; var materials = (from m in _context.Materials where m.Name.Contains(row.Cell(5).Value.ToString()) select m).ToList(); if (materials.Count > 0) { material = materials[0]; } else { material = new Materials(); material.Name = row.Cell(5).Value.ToString(); addedMaterials.Add(material); _context.Materials.Add(material); } guitar.Material = material; Types type; var types = (from t in _context.Types where t.Name.Contains(row.Cell(6).Value.ToString()) select t).ToList(); if (types.Count > 0) { type = types[0]; } else { type = new Types(); type.Name = row.Cell(6).Value.ToString(); addedTypes.Add(type); _context.Types.Add(type); } guitar.Type = type; addedGuitars.Add(guitar); _context.Guitars.Add(guitar); await _context.SaveChangesAsync(); } } catch (Exception e) { //logging самостійно :) throw new Exception("Text with context data", e); } } } } } MemoryStream memory = new MemoryStream(); using (WordprocessingDocument wordDocument = WordprocessingDocument.Create(memory, WordprocessingDocumentType.Document)) { // Add a main document part. MainDocumentPart mainPart = wordDocument.AddMainDocumentPart(); // Create the document structure and add some text. mainPart.Document = new Document(); Body body = mainPart.Document.AppendChild(new Body()); Paragraph para = body.AppendChild(new Paragraph()); Run run = para.AppendChild(new Run()); run.AppendChild(new Text("Added " + addedBrands.Count.ToString() + " Brands: ")); foreach (Brands b in addedBrands) { run.AppendChild(new Text(b.Name + ", ")); } run.AppendChild(new Break()); run.AppendChild(new Text("Added " + addedTypes.Count.ToString() + " Types: ")); foreach (Types t in addedTypes) { run.AppendChild(new Text(t.Name + ", ")); } run.AppendChild(new Break()); run.AppendChild(new Text("Added " + addedForms.Count.ToString() + " Forms: ")); foreach (Forms f in addedForms) { run.AppendChild(new Text(f.Name + ", ")); } run.AppendChild(new Break()); run.AppendChild(new Text("Added " + addedMaterials.Count.ToString() + " Materials: ")); foreach (Materials m in addedMaterials) { run.AppendChild(new Text(m.Name + ", ")); } run.AppendChild(new Break()); run.AppendChild(new Text("Added " + addedGuitars.Count.ToString() + " Guitars: ")); foreach (Guitars g in addedGuitars) { run.AppendChild(new Text(g.Name + ", ")); } mainPart.Document.Save(); } memory.Seek(0, SeekOrigin.Begin); return(File(memory, "application/vnd.openxmlformats-officedocument.wordprocessingml.document", "Report.docx")); } } return(RedirectToAction(nameof(Index))); }
public ActionResult Create(GuitarViewModel viewModel) { if (!ModelState.IsValid) { viewModel.TypeIds = _DbContext.GuitarType.ToList(); viewModel.BrandIds = _DbContext.Brand.ToList(); viewModel.Tops = _DbContext.GoTop.ToList(); viewModel.Sides = _DbContext.GoSide.ToList(); viewModel.Backs = _DbContext.GoBack.ToList(); viewModel.Necks = _DbContext.GoNeck.ToList(); viewModel.Fings = _DbContext.GoFing.ToList(); viewModel.Insurances = _DbContext.Warranty.ToList(); // return View("create", viewModel); } if (viewModel.ImageLink1 == null) { viewModel.ImageLink1 = ""; } if (viewModel.ImageLink2 == null) { viewModel.ImageLink2 = ""; } if (viewModel.ImageLink3 == null) { viewModel.ImageLink3 = ""; } if (viewModel.ImageLink4 == null) { viewModel.ImageLink4 = ""; } if (viewModel.ImageLink5 == null) { viewModel.ImageLink5 = ""; } if (viewModel.ImageLink6 == null) { viewModel.ImageLink6 = ""; } if (viewModel.VideoLink == null) { viewModel.VideoLink = ""; } //int c = _DbContext.Guitars.ToList().Count(); //int b = 0; //viewModel.GuitarId = b; //for (int i = 0; i < c; i++) //{ // var gui = _DbContext.Guitars.Single(m => m.GuitarId == i); // if (gui==null) // { // b = i; // break; // } // c = i + 1; //} var guitar = new Guitars { GuitarId = viewModel.GuitarId, MDL = viewModel.GuitarModel, BrandId = viewModel.BrandId , Availability = 0, ELE = viewModel.Electricfied, TypeId = viewModel.TypeId, MSRP = viewModel.Price, WarrId = viewModel.Insurance , ImageLink1 = viewModel.ImageLink1, ImageLink2 = viewModel.ImageLink2, ImageLink3 = viewModel.ImageLink3 , ImageLink4 = viewModel.ImageLink4, ImageLink5 = viewModel.ImageLink5, ImageLink6 = viewModel.ImageLink6 , Videolink = viewModel.VideoLink }; var guitarspec = new GuitarSpecs() { GuitarId = viewModel.GuitarId, TopId = viewModel.Top, SideId = viewModel.Side , BackId = viewModel.Back, FingId = viewModel.Back, NeckId = viewModel.Neck, Descript = viewModel.Description }; _DbContext.Guitars.Add(guitar); _DbContext.GuitarSpecs.Add(guitarspec); _DbContext.SaveChanges(); return(RedirectToAction("Index", "Home")); //try //{ // var content = _DbContext.Guitars.ToList(); // int b = content.Count(); // a.Open(); // SqlCommand x = new SqlCommand("" + // "select * from Guitars", a); // SqlDataReader danhsachtam = x.ExecuteReader(); // dt2.Load(danhsachtam); // a.Close(); // for (int i = 0; i < content.Count; i++) // { // for (int j = i + 1; j < content.Count; j++) // { // if (int.Parse((dt2.Rows[j][0]).ToString()) != int.Parse((dt2.Rows[i][0]).ToString()) + 1) // { // b = int.Parse((dt2.Rows[i][0]).ToString()) + 1; // break; // //goto done; // } // b = j + 1; // } // } // a.Open(); // string y = string.Format("insert into dbo.Guitars values('{0}','{1}','{2}','{3}','{4}','{5}','{6}','0','{7}','{8}','{9}','{10}','{11}','{12}','{13}')", // b, // viewModel.GuitarModel.ToString(), // viewModel.BrandId.ToString(), // viewModel.TypeId.ToString(), // viewModel.Price, // viewModel.Electricfied, // viewModel.Insurance, // viewModel.ImageLink1, // viewModel.ImageLink2, // viewModel.ImageLink3, // viewModel.ImageLink4, // viewModel.ImageLink5, // viewModel.ImageLink6, // viewModel.VideoLink // ); // SqlCommand vaoguitar = new SqlCommand(y, a); // string z = string.Format("insert into dbo.GuitarSpecs values('{0}','{1}','{2}','{3}','{4}','{5}','{6}')", // b, // viewModel.Top.ToString(), // viewModel.Side.ToString(), // viewModel.Back.ToString(), // viewModel.Neck.ToString(), // viewModel.Fing.ToString(), // viewModel.Description.ToString()); // SqlCommand u = new SqlCommand(z, a); // vaoguitar.ExecuteNonQuery(); // u.ExecuteNonQuery(); // a.Close(); //} //catch (Exception) //{ // return View("create", viewModel); //} //return RedirectToAction("Index", "Home"); }