private Weekdays ConvertWeekdaysToEnum(LabourerVM labourerVM) { Weekdays weekdays = 0; if (labourerVM.Sunday) { weekdays |= Weekdays.Sunday; } if (labourerVM.Monday) { weekdays |= Weekdays.Monday; } if (labourerVM.Tuesday) { weekdays |= Weekdays.Tuesday; } if (labourerVM.Wednesday) { weekdays |= Weekdays.Wednesday; } if (labourerVM.Thursday) { weekdays |= Weekdays.Thursday; } if (labourerVM.Friday) { weekdays |= Weekdays.Friday; } if (labourerVM.Saturday) { weekdays |= Weekdays.Saturday; } return(weekdays); }
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(); }
public async Task <IActionResult> PutLabourer(int id, LabourerVM labourerVM) { if (id != labourerVM.Id) { return(BadRequest()); } try { await _labourerRepo.UpdateLabourer(labourerVM); return(Ok()); } catch (Exception e) { return(BadRequest(new { message = e.Message })); } }
public async Task <ActionResult <CompanyVM> > PostLabourer(LabourerVM labourerVM) { try { var userId = _userManager.GetUserId(User); if (_labourerRepo.GetUserLabourerId(userId).Item1.HasValue) { return(BadRequest(new { message = "Labourer already exist" })); } var result = await _labourerRepo.AddLabourer(labourerVM, userId); return(Ok(result)); } catch (Exception e) { return(BadRequest(new { message = e.Message })); } }
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); }