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"));
        }
Пример #2
0
        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"));
        }