private IList<Person> Attach(IEnumerable<PersonDto> people, DataContext db) { var result = new List<Person>(); foreach (var p in people) { result.Add(db.People.Find(p.Id)); } return result; }
public DayManager(DataContext db, Day day, Week week, DateTime date) { this.db = db; this.week = week; this.day = day; this.date = date; }
public void CreateAbsence(int personId, DateTime start, DateTime end, bool isPresent = false) { using (var db = new DataContext()) { var person = db.People.Find(personId); if (person == null) { throw new EntityNotFountException(); } var absence = new Absence() { Person = person, End = end, Start = start, IsPresent = isPresent, }; new AbsenceAdapter(absence) .Validate() .ClearOccupations(db); this.RemoveFromSchedule(person, start, end, db); db.Absences.Add(absence); db.SaveChanges(); } }
public void ClearForeignKeys(DataContext db) { while (Group.People.Count > 0) { Group.People.RemoveAt(0); } var days = (from d in db.Days where d.Group.Id == this.Group.Id select d); foreach (var d in days) { db.Days.Remove(d); } db.SaveChanges(); }
public IEnumerable<LunchTimeDto> GetLunchTimes() { using (var db = new DataContext()) { return (from l in db.LunchTimes .Include(e => e.People) where l.People.Count > 0 orderby l.DayOfWeek ascending select l).ToDto(); } }
/// <summary> /// Remove persons from groups in day during the absence. In other words, /// in the time table, the absent person won't appear. /// </summary> /// <param name="db"></param> /// <returns></returns> public AbsenceAdapter ClearOccupations(DataContext db) { var days = (from day in db.Days .Include(e => e.People) where day.People.Where(p => p.Id == this.Absence.Person.Id).Count() != 0 && Absence.Start <= day.Date && day.Date <= Absence.End select day).ToList(); foreach (var day in days) { day.People.Remove(Absence.Person); } db.SaveChanges(); return this; }
public void ClearData() { using (var db = new DataContext()) { db.Absences.RemoveRange(db.Absences); db.Groups.RemoveRange(db.Groups); db.People.RemoveRange(db.People); db.Days.RemoveRange(db.Days); db.Weeks.RemoveRange(db.Weeks); db.Activities.RemoveRange(db.Activities); db.SaveChanges(); } }
public IEnumerable<ActivityDto> GetActivities(bool includeDeactivated = false) { using (var db = new DataContext()) { return (from a in db.Activities .Include(e => e.People) where a.People.Where(p => !p.IsEducator).Count() > 0 //Group without educator should be ignored && ((!includeDeactivated && a.IsActive) || includeDeactivated) select a).OrderBy(e => e.DayOfWeek) .ThenBy(e => e.MomentDay) .ThenBy(e => e.Name) .ToDto(); } }
private bool CheckFreePeriod() { using (var db = new DataContext()) { var start = this.Absence.Start; var end = this.Absence.End; if (Absence.Person == null) { this.ErrorMessage.AppendLine(Messages.Validation_NobodyToAbsence); return false; } var isValid = (from a in db.Absences where a.Person.Id == Absence.Person.Id && ( (start >= a.Start && start <= a.End) || (end >= a.Start && end <= a.End) ) select a).Count() == 0; if (!isValid) { this.ErrorMessage.AppendLine(Messages.Validation_AbsenceAtSameSpan); } return isValid; } }
public void CreateActivity(DayOfWeek day , MomentDay momentDay , IEnumerable<PersonDto> people , string name) { if (people == null) { people = new List<PersonDto>(); } using (var db = new DataContext()) { var eList = new List<Person>(); foreach (var e in people) { eList.Add(db.People.Attach(e.ToEntity())); } var activity = new Activity() { People = eList, MomentDay = momentDay, DayOfWeek = day, Name = name, }; db.Activities.Add(activity); db.SaveChanges(); } }
public UpdateEntity(DataContext db) { this.db = db; }
public void CreateGroup(GroupDto group) { group.Name = group.Name.ToLower(); using (var db = new DataContext()) { var exist = (from g in db.Groups where g.Name.ToLower() == @group.Name select g).Count() > 0; if (exist) { throw new EntityAlreadyExistException(); } db.Groups.Add(group.ToEntity()); db.SaveChanges(); } }
public ValidationStatusDto IsValidAbsence(int personId, DateTime start, DateTime end) { using (var db = new DataContext()) { var person = (from b in db.People where b.Id == personId select b).FirstOrDefault(); if (person == null) { throw new EntityNotFountException(); } var absence = new Absence() { Person = person, End = end, Start = start, }; var validator = new AbsenceValidator(absence); return validator.Validate() ? ValidationStatusDto.Valid() : ValidationStatusDto.Invalid(validator.Error); } }
public GroupDto GetGroup(string name) { name = name.ToLower(); using (var db = new DataContext()) { var result = (from g in db.Groups where g.Name.ToLower() == name select g).FirstOrDefault(); if (result == null) { return null; } db.Entry(result).Collection(e => e.People).Load(); if (result == null) { throw new EntityNotFountException(); } else { return Mapper.Map<Group, GroupDto>(result); } } }
public IEnumerable<GroupDto> GetGroups() { using (var db = new DataContext()) { var entities = (from g in db.Groups .Include(e => e.People) orderby g.Order select g).ToList(); return Mapper.Map<IEnumerable<Group>, IEnumerable<GroupDto>>(entities); } }
public void RemovePerson(int id) { using (var db = new DataContext()) { var person = db.People.Find(id); if (person == null) { return; } var groups = (from g in db.Groups where g.People.Select(e => e.Id).Contains(id) select g); foreach (var group in groups) { group.People.Remove(person); } var lunches = (from g in db.LunchTimes where g.People.Select(e => e.Id).Contains(id) select g); foreach (var lunch in lunches) { if (lunch.People != null) { lunch.People.Remove(person); } } var absences = (from a in db.Absences where a.Person.Id == id select a); db.Absences.RemoveRange(absences); var entity = db.People.Find(id); db.People.Remove(entity); db.SaveChanges(); } }
public void UpdateActivity(ActivityDto activity) { using (var db = new DataContext()) { var toRemove = db.Activities.Find(activity.Id); db.Activities.Remove(toRemove); var ePeople = new List<Person>(); var toAdd = activity.ToEntity(); foreach (var p in activity.People) { ePeople.Add(db.People.Find(p.Id)); } toAdd.People = ePeople; db.Activities.Add(toAdd); db.SaveChanges(); } }
public void RemoveGroup(int id) { using (var db = new DataContext()) { var entity = (from g in db.Groups .Include(e => e.People) where g.Id == id select g).SingleOrDefault(); if (entity != null) { new GroupAdapter(entity).ClearForeignKeys(db); db.Groups.Remove(entity); db.SaveChanges(); } } }
public void RemoveActivity(int id) { using (var db = new DataContext()) { var activity = (from a in db.Activities where a.Id == id select a).Single(); if (activity != null) { new ActivityAdapter(activity).ClearForeignKeys(db); db.Activities.Remove(activity); db.SaveChanges(); } } }
public void RemoveAbsence(int id) { using (var db = new DataContext()) { var entity = db.Absences.Find(id); if (entity != null) { db.Absences.Remove(entity); db.SaveChanges(); } } }
public PersonDto GetPerson(int id) { using (var db = new DataContext()) { var entity = db.People.Find(id); return entity.ToDto(); } }
public IEnumerable<PersonDto> GetPersons() { using (var db = new DataContext()) { var people = (from p in db.People .Include(e => e.Absences) .Include(e => e.Activities) select p).ToList(); return people.ToDto(); } }
private IList<Person> Attach(LunchTimeDto lunch, DataContext db) { return Attach(lunch.People, db); }
public void UpdatePerson(PersonDto person) { using (var db = new DataContext()) { var e = db.People.Find(person.Id); e.Name = person.Name; e.Surname = person.Surname; e.IsTrainee = person.IsTrainee; e.IsEducator = person.IsEducator; db.SaveChanges(); } }
public GroupDto GetGroup(int id) { using (var db = new DataContext()) { return (from g in db.Groups where g.Id == id select g).Single() .ToDto(); } }
private void RemoveFromSchedule(Person person, DateTime start, DateTime end, DataContext db) { start = start.Date; end = end.Date; var days = (from d in db.Days.Include(e => e.People) where d.Date >= start && d.Date <= end && d.People.Where(p => p.Id == person.Id).Count() > 0 select d); foreach (var day in days) { if (day.IsMorning && end.Hour < 12 || !day.IsMorning && end.Hour > 12) { day.People.Remove(person); } } }
public void UpdateGroup(GroupDto @group) { using (var db = new DataContext()) { var eGroup = (from g in db.Groups.Include(e => e.People) where g.Id == @group.Id select g).FirstOrDefault(); if (eGroup == null) { throw new EntityNotFountException(); } while (eGroup.People.Count() > 0) { eGroup.People.RemoveAt(0); } var benefs = new List<Person>(); foreach (var benef in group.People) { var b = db.People.Find(benef.Id); eGroup.People.Add(b); } eGroup.Name = group.Name; eGroup.Order = group.Order; db.SaveChanges(); } }
public IEnumerable<PersonDto> GetTrainees() { using (var db = new DataContext()) { return (from t in db.People where t.IsTrainee select t).ToDto(); } }
public void UpdateLunch(IEnumerable<LunchTimeDto> lunches) { using (var db = new DataContext()) { new UpdateEntity(db).Update(lunches); } }
/// <summary> /// Get free educators for the specified day and part of day. It does not take into account /// neither absences nor week schedule /// </summary> /// <param name="dayOfWeek">The day of week</param> /// <param name="isMorning"><c>True</c> for the morning. Otherwise, it is set for the afternoon</param> /// <returns>The free educators</returns> public IEnumerable<PersonDto> GetEducatorWithoutActivities(DayOfWeek dayOfWeek, bool isMorning) { var md = isMorning ? MomentDay.Morning : MomentDay.Afternoon; using (var db = new DataContext()) { var people = (from p in db.People.Include(e => e.Activities) where p.IsEducator && p.Activities.Where(e => e.DayOfWeek == dayOfWeek && (e.MomentDay & md) != 0 && e.IsActive).Count() == 0 select p).ToList(); return people.ToDto(); } }