public async Task <IActionResult> Post([FromBody] DirectorViewModel model) { try { if (ModelState.IsValid) { var director = await _userManager.FindByEmailAsync(model.DirectorEmail); if (director == null) { director = new MeticulousUser() { UserName = model.DirectorEmail, Email = model.DirectorEmail, FirstName = model.DirectorFirstName, LastName = model.DirectorLastName }; var directorResult = await _userManager.CreateAsync(director, $"{model.DirectorLastName}{DateTime.Now.Year}!"); if (directorResult == IdentityResult.Success) { await _userManager.AddToRoleAsync(director, "Director"); model.DirectorId = director.Id; var newDirector = this._mapper.Map <DirectorViewModel, Director>(model); newDirector.created_on = DateTime.Now; newDirector.modified_on = DateTime.Now; _directorRepository.AddDirector(newDirector); if (_directorRepository.SaveAll()) { return(Created($"/api/directors/{newDirector.id}", _mapper.Map <Director, DirectorViewModel>(newDirector))); } } } } else { return(BadRequest(ModelState)); } } catch (Exception e) { this._logger.LogError($"Could not save Director data: {e}"); } return(BadRequest("Failed to save Director data")); }
public async Task <IActionResult> AddAdmin([FromBody] AdminViewModel model) { try { if (ModelState.IsValid) { var admin = await userManager.FindByEmailAsync(model.AdminEmail); if (admin == null) { admin = new MeticulousUser() { UserName = model.AdminEmail, Email = model.AdminEmail, FirstName = model.AdminFirstName, LastName = model.AdminLastName }; var adminResult = await userManager.CreateAsync(admin, $"{model.AdminLastName}{DateTime.Now.Year}!"); if (adminResult == IdentityResult.Success) { await userManager.AddToRoleAsync(admin, "Admin"); return(Ok()); } } } else { return(BadRequest(ModelState)); } } catch (Exception e) { this.logger.LogError($"Could not save Admin: {e}"); } return(BadRequest("Failed to save Admin")); }
public async Task <IActionResult> Post([FromBody] MentorViewModel model) { try { if (ModelState.IsValid) { var mentorEmail = $"{model.MentorFirstName.ToLower()}.{model.MentorLastName.ToLower()}@meticulousmentoring.com"; var mentor = await this._userManager.FindByEmailAsync(mentorEmail); if (mentor == null) { mentor = new MeticulousUser() { UserName = mentorEmail, Email = mentorEmail, FirstName = model.MentorFirstName, LastName = model.MentorLastName }; var mentorResult = await _userManager.CreateAsync(mentor, $"{model.MentorLastName}{DateTime.Now.Year}!"); if (mentorResult == IdentityResult.Success) { await _userManager.AddToRoleAsync(mentor, "Mentor"); model.MentorId = mentor.Id; var newMentor = this.mapper.Map <MentorViewModel, Mentor>(model); _ctx.TimeLine.Add(new Timeline { user_id = model.MentorId, detail = "Joined Program", timeline_date = DateTime.Now }); _ctx.Addresses.Add(model.MentorAddress); await _ctx.SaveChangesAsync(); newMentor.mentees.Clear(); var mentee_names = model.MentorMentees.Select(x => x.MenteeFirstName + ' ' + x.MenteeLastName); var menteeStrings = string.Join(',', mentee_names); foreach (var mentee in model.MentorMentees) { var menteeToAdd = _ctx.Mentees.Single(x => x.id == mentee.MenteeId); menteeToAdd.modified_on = DateTime.Now; newMentor.mentees.Add(menteeToAdd); _ctx.TimeLine.Add(new Timeline { user_id = mentee.MenteeId, detail = $"Matched with {model.MentorFirstName} {model.MentorLastName}", timeline_date = DateTime.Now }); } _ctx.TimeLine.Add(new Timeline { user_id = model.MentorId, detail = $"Matched with mentee(s) {menteeStrings}", timeline_date = DateTime.Now }); newMentor.email = mentorEmail; newMentor.created_on = DateTime.Now; newMentor.modified_on = DateTime.Now; this.mentorRepository.AddMentor(newMentor); if (this.mentorRepository.SaveAll()) { return(Created( $"/api/mentors/{newMentor.id}", this.mapper.Map <Mentor, MentorViewModel>(newMentor))); } } } } else { return(this.BadRequest(ModelState)); } } catch (Exception e) { this.logger.LogError($"Could not save Mentor data: {e}"); } return(this.BadRequest("Failed to save new Mentor data")); }
public async Task Seed() { this.ctx.Database.EnsureCreated(); string[] roles = { "Admin", "Director", "Mentor", "Mentee" }; foreach (var role in roles) { var roleExist = await this.roleManager.RoleExistsAsync(role); if (!roleExist) { await this.roleManager.CreateAsync(new IdentityRole <int>(role)); } } //// Add Admin User var user = await this.userManager.FindByEmailAsync("*****@*****.**"); if (user == null) { user = new MeticulousUser() { UserName = "******", Email = "*****@*****.**" }; var result = await this.userManager.CreateAsync(user, "War3agle!"); if (result != IdentityResult.Success) { throw new InvalidOperationException("Failed to create default user"); } else { await this.userManager.AddToRoleAsync(user, "Admin"); } } //// End Add Admin User ////Add Director var director = await this.userManager.FindByEmailAsync("*****@*****.**"); if (director == null) { director = new MeticulousUser() { UserName = "******", Email = "*****@*****.**" }; var directorResult = await this.userManager.CreateAsync(director, "War3agle!"); if (directorResult == IdentityResult.Success) { await this.userManager.AddToRoleAsync(director, "Director"); } } ////End Add Director ////Add Mentee var mentee = await this.userManager.FindByEmailAsync("*****@*****.**"); if (mentee == null) { mentee = new MeticulousUser() { UserName = "******", Email = "*****@*****.**", FirstName = "Ty", LastName = "Wright" }; var menteeResult = await this.userManager.CreateAsync(mentee, "War3agle!"); if (menteeResult == IdentityResult.Success) { await this.userManager.AddToRoleAsync(mentee, "Mentee"); var menteeAddress = new Address() { address1 = "709 Stonewall Drive", city = "Irondale", state = "AL", zip = "35210" }; var menteeSchoolAddress = new Address() { address1 = "6100 Old Leeds Rd", city = "Irondale", state = "AL", zip = "35210" }; var menteeSchool = new School() { school_name = "Shades Valley High School", address = menteeSchoolAddress, principal = "Antjuan Marsh", system = ctx.Systems.Find(1) }; var newMentee = new Mentee() { id = mentee.Id, first_name = "Ty", middle = "Kristopher", last_name = "Wright", gender = "M", dob = Convert.ToDateTime("2000-08-29T00:00:00"), email = "*****@*****.**", created_on = Convert.ToDateTime("2017-12-13T00:00:00"), modified_on = Convert.ToDateTime("2017-12-13T00:00:00"), is_active = true, address = menteeAddress, school = menteeSchool, classification = ctx.Classifications.Find(4) }; this.ctx.Addresses.Add(menteeAddress); this.ctx.Addresses.Add(menteeSchoolAddress); this.ctx.Schools.Add(menteeSchool); this.ctx.Mentees.Add(newMentee); this.ctx.SaveChanges(); } } ////End Add Mentee ////Add Mentor var mentor = await this.userManager.FindByEmailAsync("*****@*****.**"); if (mentor == null) { mentor = new MeticulousUser() { UserName = "******", Email = "*****@*****.**", FirstName = "Joe", LastName = "Mentor" }; var mentorResult = await this.userManager.CreateAsync(mentor, "War3agle!"); if (mentorResult == IdentityResult.Success) { await this.userManager.AddToRoleAsync(mentor, "Mentor"); var mentorAddress = new Address() { address1 = "1254 Mentor Way", city = "Birmingham", state = "AL", zip = "35207" }; var newMentor = new Mentor() { id = mentor.Id, first_name = "Joe", last_name = "Mentor", gender = "M", address = mentorAddress, is_active = true, created_on = Convert.ToDateTime(DateTime.Now), modified_on = Convert.ToDateTime(DateTime.Now), mentees = null }; this.ctx.Addresses.Add(mentorAddress); this.ctx.Mentors.Add(newMentor); this.ctx.SaveChanges(); } }//// End Add Mentor ////Add Guardian var guardian = ctx.Guardians.Single(x => x.email == "*****@*****.**"); if (guardian == null) { guardian = new Guardian() { first_name = "Anthony", last_name = "Wright II", address = ctx.Addresses.SingleOrDefault(x => x.address1 == "709 Stonewall Dr"), gender = "M", email = "*****@*****.**", middle = "Q", children = null }; this.ctx.Guardians.Add(guardian); this.ctx.SaveChanges(); } //// End Add Guardian ////Update Mentee var updatedMentee = ctx.Mentees.SingleOrDefault(x => x.email == "*****@*****.**"); var addedMentor = ctx.Mentors.SingleOrDefault(x => x.last_name == "Mentor"); var addedGuardian = ctx.Guardians.SingleOrDefault(x => x.email == "*****@*****.**"); if (addedMentor.mentees == null) { addedMentor.mentees = new List <Mentee>(); addedMentor.mentees.Add(updatedMentee); this.ctx.Mentors.Update(addedMentor); this.ctx.SaveChanges(); } if (addedGuardian.children == null) { addedGuardian.children = new List <Mentee>(); addedGuardian.children.Add(updatedMentee); this.ctx.Guardians.Update(addedGuardian); this.ctx.SaveChanges(); } }
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")); }