public async Task UpdateLabourer(LabourerVM labourerVM)
        {
            var labourer = _context.Labourers.Include(l => l.LabourerSkills).FirstOrDefault(l => l.Id == labourerVM.Id);

            if (labourer == null)
            {
                throw new KeyNotFoundException();
            }

            if (labourer != null)
            {
                labourer.Id           = labourer.Id;
                labourer.FirstName    = labourerVM.FirstName;
                labourer.LastName     = labourerVM.LastName;
                labourer.PersonalId   = labourerVM.PersonalId;
                labourer.City         = labourerVM.City;
                labourer.Province     = labourerVM.Province;
                labourer.Country      = labourerVM.Country;
                labourer.Address      = labourerVM.Address;
                labourer.Phone        = labourerVM.Phone;
                labourer.IsActive     = labourerVM.IsActive;
                labourer.Availability = ConvertWeekdaysToEnum(labourerVM);
            }

            if (labourer.LabourerSkills != null)
            {
                var skillsToDelete = labourer.LabourerSkills.Where(s => !labourerVM.Skills.Any(ls => ls.Id == s.SkillId)).ToList();
                if (skillsToDelete != null && skillsToDelete.Count > 0)
                {
                    _context.LabourerSkills.RemoveRange(skillsToDelete);
                }
            }

            var newSkills = labourerVM.Skills.Where(s => !labourer.LabourerSkills.Any(ls => ls.SkillId == s.Id)).ToList();

            if (newSkills != null && newSkills.Count > 0)
            {
                foreach (var skill in newSkills)
                {
                    var newSkill = new LabourerSkill
                    {
                        LabourerId = labourer.Id,
                        SkillId    = skill.Id.Value
                    };
                    _context.Add(newSkill);
                }
            }

            await UpdateUserEmail(labourer.UserId, labourerVM.Email);

            _context.Update(labourer);
            _context.SaveChanges();
        }
Ejemplo n.º 2
0
        public IActionResult PutLabourerProfile(LabourerProfileVM labourerProfile)
        {
            var lp = _context.Labourer.SingleOrDefault(l => l.LabourerId == labourerProfile.Labourer.LabourerId);

            if (lp == null)
            {
                return(NotFound());
            }
            else
            {
                lp.LabourerFirstName = labourerProfile.Labourer.LabourerFirstName;
                lp.LabourerLastName  = labourerProfile.Labourer.LabourerLastName;
                lp.IsAvailable       = labourerProfile.Labourer.IsAvailable;
                lp.LabourerEmail     = labourerProfile.Labourer.LabourerEmail;
            }

            _context.LabourerSkill.RemoveRange(_context.LabourerSkill.Where(al => al.LabourerId == labourerProfile.Labourer.LabourerId));

            foreach (string skillName in labourerProfile.Skills.Select(s => s.SkillName))
            {
                Skill skill = _context.Skill.Where(s => s.SkillName == skillName).FirstOrDefault();
                if (skill != null)
                {
                    LabourerSkill labourerSkill = new LabourerSkill
                    {
                        SkillId    = skill.SkillId,
                        LabourerId = labourerProfile.Labourer.LabourerId
                    };
                    _context.LabourerSkill.Add(labourerSkill);
                    _context.SaveChanges();
                }
                else
                {
                    return(NotFound());
                }
            }

            try
            {
                _context.SaveChanges();
            }
            catch (DbUpdateException)
            {
                throw;
            }
            return(NoContent());
        }
        private static async Task CreateLabourer(string firstName, string lastName, string phone, string address, Weekdays availability, RecruitmentSystemContext context, UserManager <SystemUser> userManager, string role)
        {
            var email = firstName + "@" + lastName + ".com";
            var user  = await CreateUserWithRole(context, userManager, new SystemUser { Email = email, UserName = email }, role);

            var labourer = new Labourer
            {
                FirstName    = firstName,
                LastName     = lastName,
                Phone        = phone,
                Address      = address,
                City         = "Vancouver",
                Province     = "BC",
                Country      = "Canada",
                IsActive     = true,
                PersonalId   = "123456789",
                User         = user,
                Availability = availability
            };

            context.Labourers.Add(labourer);

            //add skills
            var    skills = context.Skills.ToList();
            Random r      = new Random();
            var    from   = r.Next(0, 14);
            var    to     = r.Next(from, 14);
            var    count  = 1;

            for (var i = from; i <= to; i++)
            {
                if (count == 4)
                {
                    break;
                }
                var labourerSkill = new LabourerSkill
                {
                    Labourer = labourer,
                    Skill    = skills[i]
                };
                context.LabourerSkills.Add(labourerSkill);
                count++;
            }
            context.SaveChanges();
        }
        public async Task <LabourerVM> AddLabourer(LabourerVM labourerVM, string userId)
        {
            var labourer = new Labourer
            {
                UserId       = userId,
                FirstName    = labourerVM.FirstName,
                LastName     = labourerVM.LastName,
                PersonalId   = labourerVM.PersonalId,
                City         = labourerVM.City,
                Province     = labourerVM.Province,
                Country      = labourerVM.Country,
                Address      = labourerVM.Address,
                Phone        = labourerVM.Phone,
                IsActive     = labourerVM.IsActive,
                Availability = ConvertWeekdaysToEnum(labourerVM)
            };

            _context.Add(labourer);
            var labourerSkills = new List <LabourerSkill>();

            foreach (var skill in labourerVM.Skills)
            {
                var newSkill = new LabourerSkill
                {
                    Labourer = labourer,
                    SkillId  = skill.Id.Value
                };
                labourerSkills.Add(newSkill);
                _context.LabourerSkills.Add(newSkill);
            }

            labourer.LabourerSkills = labourerSkills;
            await UpdateUserEmail(userId, labourerVM.Email);

            _context.SaveChanges();
            labourerVM.Id = labourer.Id;
            return(labourerVM);
        }
        public async Task <JsonResult> OnPostAsync([FromBody] LabourerRegisterVM input)
        {
            var user = new IdentityUser {
                UserName = input.User.Email.ToLower(), Email = input.User.Email
            };
            var result = await _userManager.CreateAsync(user, input.User.Password);

            var errorList = new List <string>();

            dynamic jsonResponse = new JObject();

            if (result.Succeeded)
            {
                SystemUser sysUser = new SystemUser()
                {
                    Email = input.User.Email,
                    Role  = "Labourer"
                };

                Labourer labourer = new Labourer
                {
                    LabourerFirstName = input.Labourer.LabourerFirstName,
                    LabourerLastName  = input.Labourer.LabourerLastName,
                    LabourerSin       = input.Labourer.LabourerSin,
                    LabourerEmail     = input.User.Email,
                    IsAvailable       = true,
                    OnLeave           = false
                };
                _context.SystemUser.Add(sysUser);
                sysUser.Labourer.Add(labourer);
                _context.SaveChanges();

                //if (input.AvailableDays != null)
                //{

                //    foreach (string day in input.AvailableDays)
                //    {
                //        Availability availability = _context.Availability.Where(a => a.AvailabilityDay == day).FirstOrDefault();

                //        AvailabilityLabourer availabilityLabourer = new AvailabilityLabourer
                //        {
                //            AvailabilityId = availability.AvailabilityId,
                //            LabourerId = labourer.LabourerId
                //        };
                //        _context.AvailabilityLabourer.Add(availabilityLabourer);
                //        _context.SaveChanges();
                //    }
                //}
                //else
                //{
                //    await _userManager.DeleteAsync(user);
                //    _context.Labourer.Remove(labourer);
                //    _context.SystemUser.Remove(sysUser);
                //    _context.SaveChanges();
                //    jsonResponse.status = "Available day is not valid";
                //    jsonResponse.token = " ";
                //    return Json(jsonResponse);
                //}


                foreach (int skillId in input.Skills)
                {
                    LabourerSkill labourerSkill = new LabourerSkill
                    {
                        SkillId    = skillId,
                        LabourerId = labourer.LabourerId
                    };
                    _context.LabourerSkill.Add(labourerSkill);
                    _context.SaveChanges();
                }

                AuthRepo registerRepo = new AuthRepo(_signInManager, _config, _serviceProvider, _context);
                var      tokenString  = registerRepo.GenerateJSONWebToken(user);
                jsonResponse.token  = tokenString;
                jsonResponse.status = "OK";
                jsonResponse.role   = "Labourer";
                jsonResponse.email  = sysUser.Email;
                jsonResponse.id     = labourer.LabourerId;
                jsonResponse.name   = labourer.LabourerFirstName + ' ' + labourer.LabourerLastName;
                return(Json(jsonResponse));
            }

            foreach (IdentityError error in result.Errors)
            {
                errorList.Add(error.Description);
            }
            jsonResponse.status      = 422;
            jsonResponse.description = errorList[0];
            jsonResponse.token       = "";
            return(Json(jsonResponse));
        }