public ActionResult Create(MenteeViewModel viewModel) { if (!ModelState.IsValid) { var errors = ModelState.Where(v => v.Value.Errors.Any()); return(View(viewModel)); } // New person that represents the mentee Person mentee = new Person(viewModel.Mentee); // Make sure we set the user as Mentee mentee.PersonTypeId = DomainHelper.GetIdByKeyValue(ref this._db, "PersonType", "Mentee"); mentee.StatusId = DomainHelper.GetIdByKeyValue(ref _db, "StatusCode", "Active"); this._db.People.Add(mentee); // Home Address Address homeAddr = new Address(viewModel.HomeAddress); PersonAddress p2a = new PersonAddress(mentee.id, homeAddr.id, DomainHelper.GetIdByKeyValue(ref _db, "AddressType", "Home")); this._db.Addresses.Add(homeAddr); this._db.PersonToAddress.Add(p2a); // Save changes this._db.SaveChanges(); return(RedirectToAction("Index")); }
public ActionResult Edit(int id) { Person mentee = PersonHelper.GetPersonById(ref this._db, id); Address homeAddress = AddressHelper.GetAddressByPersonAndType(ref this._db, mentee.id, "Home"); MenteeViewModel viewModel = new MenteeViewModel { Mentee = mentee, HomeAddress = homeAddress }; return(View(viewModel)); }
public ActionResult Edit(MenteeViewModel viewModel) { if (!ModelState.IsValid) { var errors = ModelState.Where(v => v.Value.Errors.Any()); return(View(viewModel)); } // Find the existing Person object Person mentee = this._db.People.Find(viewModel.Mentee.id); if (mentee != null) { // Update the Person object with the new details mentee.Copy(viewModel.Mentee); mentee.PersonTypeId = DomainHelper.GetIdByKeyValue(ref this._db, "PersonType", "Mentee"); } else { // New address record this._db.People.Add(new Person(viewModel.Mentee)); } // Find the home address Address homeAddr = this._db.Addresses.Find(viewModel.HomeAddress.id); if (homeAddr != null) { homeAddr.Copy(homeAddr); } else { homeAddr = this._db.Addresses.Add(new Address(viewModel.HomeAddress)); this._db.PersonToAddress.Add( new PersonAddress(mentee.id, homeAddr.id, DomainHelper.GetIdByKeyValue(ref this._db, "AddressType", "Home"))); } // Update the database this._db.SaveChanges(); // Done, go back to the list of mentors return(RedirectToAction("Index")); }
public async Task <IActionResult> Post([FromBody] MenteeViewModel model) { try { if (ModelState.IsValid) { var mentee = await _userManager.FindByEmailAsync(model.MenteeEmail); if (mentee == null) { mentee = new MeticulousUser() { UserName = model.MenteeEmail, Email = model.MenteeEmail, FirstName = model.MenteeFirstName, LastName = model.MenteeLastName }; var menteeResult = await _userManager.CreateAsync(mentee, model.MenteeLastName + DateTime.Now.Year + "!"); if (menteeResult == IdentityResult.Success) { await _userManager.AddToRoleAsync(mentee, "Mentee"); model.MenteeId = mentee.Id; var newMentee = this.mapper.Map <MenteeViewModel, Mentee>(model); _ctx.Addresses.Add(model.MenteeAddress); await _ctx.SaveChangesAsync(); var mClassification = _ctx.Classifications.Single(x => x.classification_id == model.MenteeClassification.classification_id); newMentee.classification = mClassification; var mSchool = _ctx.Schools.Single(s => s.id == model.MenteeSchool.id); newMentee.school = mSchool; newMentee.created_on = DateTime.Now; newMentee.modified_on = DateTime.Now; using (BinaryReader br = new BinaryReader(model.MenteeImageFile)) { var bytes = br.ReadBytes((int)model.MenteeImageFile.Length); _ctx.Images.Add(new Image { user_id = model.MenteeId, filename = model.MenteeImageFile.Name, content_type = Path.GetExtension(model.MenteeImageFile.Name), data = bytes }); } this.menteeRepository.AddMentee(newMentee); if (this.menteeRepository.SaveAll()) { _ctx.TimeLine.Add(new Timeline { user_id = model.MenteeId, detail = "Started Program", timeline_date = DateTime.Now }); _ctx.SaveChanges(); return(Created($"/api/mentees/{newMentee.id}", this.mapper.Map <Mentee, MenteeViewModel>(newMentee))); } } } } else { return(this.BadRequest(ModelState)); } } catch (Exception e) { this.logger.LogError($"Could not save Mentee data {e}"); } return(this.BadRequest("Failed to save new Mentee data")); }