public IActionResult AddRegion(SaveRegionViewModel model) { using (var stream = System.IO.File.Create($"wwwroot/img/{model.Name}.jpg")) { model.File.CopyTo(stream); } Image img = new Image() { Name = model.Name, Url = $"img/{model.Name}.jpg" }; Context.Images.Add(img); Context.SaveChanges(); Region region = new Region() { Name = model.Name, Description = model.Description, PostionLat = model.PostionLat, PostionLng = model.PostionLng, Photo = img }; Context.Regions.Add(region); Context.SaveChanges(); return(RedirectToAction("panel", "admin")); }
public IActionResult Save(IKDDSModel ikddsmodel) { if (!ModelState.IsValid) { return(View("Edit")); } var toUpdate = _context.IKDDSModels.SingleOrDefault(t => t.Id == ikddsmodel.Id); toUpdate.OfficerName = ikddsmodel.OfficerName; toUpdate.RpsNumber = ikddsmodel.RpsNumber; toUpdate.RsdNumber = ikddsmodel.RsdNumber; toUpdate.EntryData = ikddsmodel.EntryData; toUpdate.FinalData = ikddsmodel.FinalData; toUpdate.FinalOfInvestigation = ikddsmodel.FinalOfInvestigation; toUpdate.IKDDS = ikddsmodel.IKDDS; toUpdate.OZDate = ikddsmodel.OZDate; toUpdate.ArticleNumber = ikddsmodel.ArticleNumber; toUpdate.RealEndDate = ikddsmodel.RealEndDate; _context.SaveChanges(); var invest = _context.IKDDSModels.Include(x => x.InvestigationType).ToList(); var viewModel = new MainTableViewModel { IKDDSModels = invest, }; return(View("MainTable", viewModel)); }
public async Task <IActionResult> Add(MessageViewModel model) { //returnUrl = GetValidUrl(returnUrl, "POST"); //ViewBag.ReturnUrl = returnUrl; var returnUrl = Request.Form["returnUrl"].ToString(); if (ModelState.IsValid) { try { if (model.Id != 0) { return(BadRequest()); } var channel = _context.Channels.SingleOrDefault(x => x.Id == model.ChannelId); if (channel == null) { return(BadRequest()); } var currentUser = await _userManager.GetUserAsync(User); if (model.CreatedById != currentUser.Id) { TempData["Message"] = "Niespójność użytkowników w modelu"; throw new Exception("Niespójność użytkowników w modelu"); } var newMessage = new MessageModel() { Id = model.Id, Text = model.Text, DateAdded = DateTime.Now, ChannelId = model.ChannelId, CreatedById = model.CreatedById }; _context.Messages.Add(newMessage); _context.SaveChanges(); TempData["Message"] = $"Dodano nową wiadomość do kanału: {channel.Name}"; //return RedirectToAction("Index"); return(Redirect(returnUrl)); } catch (Exception e) { Console.WriteLine(e); throw; } } return(View(model)); }
public ActionResult <CustomerDbo> CreateCustomer(CustomerDbo customerDTO) { var customer = _mapper.Map <CustomerDbo, Customer>(customerDTO); if (!ModelState.IsValid) { return(BadRequest()); } _context.Customers.Add(customer); _context.SaveChanges(); customerDTO.Id = customer.Id; return(NoContent()); }
public IActionResult Save(Movie movie) { if (!ModelState.IsValid) { var viewModel = new NewMovieFormViewModel(movie) { Genres = _context.Genre.ToList() }; return(View("MoviesForm", viewModel)); } if (movie.Id == 0) // sprawdzamy czy movie jest nowy { _context.Movies.Add(movie); } else // jeżeli nie wyszukujemy w bazie { var movieInDb = _context.Movies.Single(x => x.Id == movie.Id); // TryUpdateModelAsync(customer); //przypisyjemy ręcznie lub alternatywa AutoMapper Mapper.Map(customer,customerInDb) movieInDb.Name = movie.Name; movieInDb.ReleaseDate = movie.ReleaseDate; movieInDb.GenreId = movie.GenreId; movieInDb.NumberInStock = movie.NumberInStock; } _context.SaveChanges(); return(RedirectToAction("Index", "Movies")); }
public IActionResult IKDDSForm(IKDDSModel ikddsmodel) { if (!ModelState.IsValid) { return(View("New")); } if (ikddsmodel.RpsNumber != null) { ikddsmodel.FinalData = ikddsmodel.EntryData.AddDays(30); //ikddsmodel.InvestigationType.Type; } else if (ikddsmodel.RsdNumber != null) { ikddsmodel.FinalData = ikddsmodel.EntryData.AddDays(60); // ikddsmodel.InvestigationType.Type = "RSD"; } else if ((ikddsmodel.RpsNumber != null) && (ikddsmodel.RsdNumber != null)) { ikddsmodel.FinalData = ikddsmodel.EntryData.AddDays(60); //ikddsmodel.InvestigationType.Type = "RPS/RSD"; } ikddsmodel.AddedInSystem = DateTime.Now; ikddsmodel.OZDate = null; ikddsmodel.RealEndDate = null; _context.IKDDSModels.Add(ikddsmodel); _context.SaveChanges(); return(RedirectToAction("MainTable", "Summary")); }
public IActionResult Edit(int id, IndustryModel industryModel) { if (id != industryModel.ID) { return(NotFound()); } if (ModelState.IsValid) { try { _context.Industries.Update(industryModel); _context.SaveChanges(); } catch (DbUpdateConcurrencyException) { if (!IndustryModelExists(industryModel.ID)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } return(View(industryModel)); }
[ValidateAntiForgeryToken] //zabezpieczenie danych z formularza public IActionResult Save(Customer customer) { if (!ModelState.IsValid) { var viewModel = new NewCustomerViewModel { Customer = customer, MembershipType = _context.MembershipType.ToList() }; return(View("CustomerForm", viewModel)); } if (customer.Id == 0) // sprawdzamy czy customer jest nowym użytkownikiem { _context.Customers.Add(customer); } else // jeżeli nie wyszukujemy w bazie { var customerInDb = _context.Customers.Single(x => x.Id == customer.Id); // TryUpdateModelAsync(customer); //przypisyjemy ręcznie lub alternatywa AutoMapper Mapper.Map(customer,customerInDb) customerInDb.Name = customer.Name; customerInDb.BirthDate = customer.BirthDate; customerInDb.MembershipTypeId = customer.MembershipTypeId; customerInDb.IsSubscribedToNewsletter = customer.IsSubscribedToNewsletter; } _context.SaveChanges(); return(RedirectToAction("Index", "Customers")); }
public async Task <IActionResult> CreateHero(HeroModel heroModel) { heroModel.User = await UserManager.GetUserAsync(User); _context.Add(heroModel); _context.SaveChanges(); return(RedirectToAction("Account", "Account")); }
public IActionResult Add(ChannelModel model) { TempData["Message"] = $"Dodawanie nowego kanału nie powiodło się"; try { if (model.Id != 0) { return(BadRequest()); } var currentUser = _userManager.Users.SingleOrDefault(x => x.UserName == User.Identity.Name); if (currentUser == null) { throw new Exception("Nie udało się pobrać zalogowanego użytkownika"); } if (ModelState.IsValid) { model.DateAdded = DateTime.Now; model.ChannelColor = ColorHelper.GetRandomColor(); model.IsDefault = false; model.CreatedById = currentUser.Id; _context.Channels.Add(model); _context.SaveChanges(); TempData["Message"] = $"Dodawanie nowego kanału zakończyło się powodzeniem"; return(RedirectToAction("Index")); } } catch (Exception e) { Console.WriteLine(e); throw; } return(View(model)); }
/// <summary> /// Wypełnia bazę w pamięci przykładowymi danymi. Dodatkowo wyświetla dodane dane w konsoli. /// </summary> protected void DbInit() { using (var context = new EFCContext()) { var bookFaker = new Faker <BookModel>(); bookFaker.RuleFor(x => x.Title, faker => faker.Lorem.Word().ToUpperFirstLetter()); bookFaker.RuleFor(x => x.Author, faker => $"{faker.Person.FirstName} {faker.Person.LastName}"); var bookModels = bookFaker.Generate(6); foreach (var b in bookModels) { Console.WriteLine($"Book: {b.ID}, {b.Title}, {b.Author}"); } var readerFaker = new Faker <ReaderModel>(); readerFaker.RuleFor(x => x.Name, faker => $"{faker.Person.FirstName} {faker.Person.LastName}"); readerFaker.RuleFor(x => x.Age, faker => faker.Random.Number(10, 100)); var readerModels = readerFaker.Generate(8); foreach (var r in readerModels) { Console.WriteLine($"Reader: {r.ID}, {r.Name}, {r.Age}"); } var bookReaderFaker = new Faker <BookReaderModel>(); bookReaderFaker.RuleFor(x => x.BookID, faker => faker.IndexFaker); bookReaderFaker.RuleFor(x => x.ReaderID, faker => faker.IndexFaker); // bookReaderFaker.RuleFor(x => x.LendDate, faker => faker.Date.Past()); bookReaderFaker.RuleFor(x => x.LendDate, faker => faker.Date.Past().ToLocalTime()); var bookReaderModels = bookReaderFaker.Generate(2); //var lendDate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); foreach (var br in bookReaderModels) { Console.WriteLine($"Book-Reader: {br.ID}, {br.BookID}, {br.ReaderID}, {br.LendDate}"); } context.Books.AddRange(bookModels); context.Readers.AddRange(readerModels); context.BookReader.AddRange(bookReaderModels); context.SaveChanges(); } }
private static void AddTestData(EFCContext context) { var clients = new[] { new CLIENT { CLIENT_ID = "CL99", CLIENT_NAME = "Client 99" }, //new CLIENT {CLIENT_ID ="CL80", CLIENT_NAME ="Client 80"} }; var clientContacts = new[] { new CLIENT_CONTACT { CLIENT_ID = "CL99", CONTACT_ID = 1, CONTACT_NAME = "Contact 1", CONTACT_EMAIL = "*****@*****.**" }, new CLIENT_CONTACT { CLIENT_ID = "CL99", CONTACT_ID = 2, CONTACT_NAME = "Contact 2", CONTACT_EMAIL = "*****@*****.**" }, new CLIENT_CONTACT { CLIENT_ID = "CL99", CONTACT_ID = 3, CONTACT_NAME = "Contact 3", CONTACT_EMAIL = "*****@*****.**" }, new CLIENT_CONTACT { CLIENT_ID = "CL99", CONTACT_ID = 4, CONTACT_NAME = "Contact 4", CONTACT_EMAIL = "*****@*****.**" }, }; var orders = new[] { new ORDER { ORDER_ID = 1, CLIENT_ID = "CL99", CREATION_DATE = DateTime.Now.Subtract(TimeSpan.FromDays(10)) }, new ORDER { ORDER_ID = 2, CLIENT_ID = "CL99", CREATION_DATE = DateTime.Now.Subtract(TimeSpan.FromDays(5)) }, new ORDER { ORDER_ID = 3, CLIENT_ID = "CL99", CREATION_DATE = DateTime.Now.Subtract(TimeSpan.FromDays(2)) }, new ORDER { ORDER_ID = 4, CLIENT_ID = "CL99", CREATION_DATE = DateTime.Now }, }; var orderDetails = new[] { new ORDER_DETAIL { ORDER_ID = 1, CLIENT_ID = null, BILLING_TYPE = "B0" }, //no contact information new ORDER_DETAIL { ORDER_ID = 2, CLIENT_ID = "CL99", BILLING_CONTACT_ID = 1, SHIPPING_CONTACT_ID = 2, BILLING_TYPE = "B0" }, // Both contacts set new ORDER_DETAIL { ORDER_ID = 3, CLIENT_ID = "CL99", SHIPPING_CONTACT_ID = 3, BILLING_TYPE = "B1" }, //only Shipping contact new ORDER_DETAIL { ORDER_ID = 4, CLIENT_ID = "CL99", BILLING_CONTACT_ID = 4, BILLING_TYPE = "B3" }, //only billing contact }; try { context.AddRange(clients); context.AddRange(clientContacts); context.AddRange(orders); context.AddRange(orderDetails); var result = context.SaveChanges(); Console.WriteLine($"Test data added to {context.Database.ProviderName}, result => {result}"); } catch (Exception ex) { Console.WriteLine($"Failed {nameof(AddTestData)} {ex}"); } }
public bool Create(DepartmentModel department) { _context.Departments.Add(department); return(_context.SaveChanges() > 0); }