public async Task <IActionResult> DeleteUser(int id)
        {
            if (await _accountRepository.deleteUser(id))
            {
                var timelineEntries = _ctx.TimeLine.Where(x => x.user_id == id).ToList();
                _ctx.TimeLine.RemoveRange(timelineEntries);

                var user = await userManager.FindByIdAsync(id.ToString());

                if (user != null)
                {
                    if (_ctx.SaveChanges() > -1)
                    {
                        await userManager.DeleteAsync(user);

                        return(Ok());
                    }
                    else
                    {
                        return(BadRequest("Couldn't delete user"));
                    }
                }
                else
                {
                    return(NotFound("User not found!"));
                }
            }
            else
            {
                return(BadRequest("Could not delete user related data"));
            }
        }
Example #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"));
        }