public ActionResult Edit(LoggedJournalEditVm vm, string car, string debit) { Journal j = new Journal(); if(ModelState.IsValid) { // retrives the car from db var dbcar = GetCar(); //building journal object, values is from the posted form j = new Journal { JournalId = vm.JournalId, Travelers = vm.Travelers, ProjectNumber = vm.ProjectNumber, OdometerStart = vm.OdometerStart, OdometerEnd = vm.OdometerEnd, StartDate = Convert.ToDateTime(vm.StartDate), EndDate = Convert.ToDateTime(vm.EndDate), FromDestination = vm.FromDestination, ToDestination = vm.ToDestination, Debit = Convert.ToInt16(debit), KmNo = vm.KmNo, Purpose = vm.Purpose, SavedNotSent = vm.SavedNotSent, UserId = vm.UserId, Regno = dbcar.Regno, }; db.Entry(j).State = EntityState.Modified; db.SaveChanges(); return RedirectToAction("LoggedJournals"); } //ViewBag.UserId = new SelectList(db.Users, "UserId", "Email", journal.UserId); LoggedJournalEditVm vm1 = service.GetLoggedJournalVm(j); return View(vm1); }
/// <summary> /// method to find a single journal. Used in editing. /// </summary> /// <param name="id">Journal to be found</param> /// <returns>Journal</returns> public Journal FindSingleJournal(int? id) { Journal journal = new Journal(); journal = db.Journals.Find(id); return journal; }
/// <summary> /// Saves a journal to DB, this journal is just saved and not completed. /// SavedNotSent == 1 /// </summary> /// <param name="vm">create journal based on vm values</param> /// <param name="project">project to be stored in journal</param> /// <param name="userId">userId to be stored in journal</param> /// <param name="debit">debit to be stored in journal</param> /// <param name="regno">regno to be stored in journal</param> public void SaveJournal(ViewModel.JournalVM vm, string project, int userId, string debit, string regno) { if (vm.EndDate == null) { vm.EndDate = DateTime.Now.ToString("yyyy-MM-dd"); } //converts string to int int odometerEnd; Int32.TryParse(vm.OdometerEnd, out odometerEnd); Journal journal = new Journal { UserId = userId, Travelers = vm.Travelers, ProjectNumber = project, OdometerStart = vm.OdometerStart, OdometerEnd = odometerEnd, StartDate = Convert.ToDateTime(vm.StartDate), EndDate = Convert.ToDateTime(vm.EndDate), FromDestination = vm.From, ToDestination = vm.To, Debit = Convert.ToInt16(debit), Purpose = vm.Purpose, Regno = regno, SavedNotSent = 1 // when saved == 1 }; //if a journal alredi exist, we want to update it // retrive the journal with column SavedNotSent == 1 depening on user var listJournals = from u in db.Users join j in db.Journals on u.UserId equals j.UserId where (u.UserId == userId) && (j.SavedNotSent == 1) select j; if (listJournals.Any()) { var originalJournal = listJournals.First(); journal.JournalId = originalJournal.JournalId; db.Entry(originalJournal).CurrentValues.SetValues(journal); db.SaveChanges(); } else { //saves journal to db db.Journals.Add(journal); db.SaveChanges(); } }
/// <summary> /// Method for bulk updating the journal /// </summary> /// <param name="journal">Journal to be updated</param> public void UpdateJournal(Journal journal) { db.Journals.Attach(journal); var entry = db.Entry(journal); entry.State = System.Data.Entity.EntityState.Modified; //entry.Property(e => e.JournalId).IsModified = false; //id will never be changed db.SaveChanges(); }
/// <summary> /// assembles a JournalVM /// </summary> /// <param name="journal">Saved journal</param> /// <returns>Journal-viewmodel</returns> public JournalVM GetSavedJournal(Journal journal) { string odometerEnd; if (journal.OdometerEnd == 0) { odometerEnd = ""; } else { odometerEnd = journal.OdometerEnd.ToString(); } JournalVM vm = new JournalVM { Travelers = journal.Travelers, Projects = GetProjects(journal.UserId, journal.ProjectNumber), OdometerStart = journal.OdometerStart, OdometerEnd = odometerEnd, From = journal.FromDestination, To = journal.ToDestination, Purpose = journal.Purpose, Cars = GetCars(journal.Regno), Debits = GetDebit(journal.Debit), JournalId = journal.JournalId, StartDate = journal.StartDate.ToString("yyyy-MM-dd"), EndDate = journal.EndDate.ToString("yyyy-MM-dd") }; return vm; }