/// <summary> /// Retrieve a single department /// </summary> /// <param name="performingUserId">user making the request</param> /// <param name="departmentId">department to retrieve</param> /// <returns></returns> public IDepartmentDto GetDepartment(int performingUserId, int departmentId) { using (var context = new PrometheusContext()) { return(ManualMapper.MapDepartmentToDto(context.Departments.FirstOrDefault(x => x.Id == departmentId))); } }
/// <summary> /// Retrieves all departments /// </summary> /// <param name="performingUserId">user making the request</param> /// <returns></returns> public IEnumerable <IDepartmentDto> GetDepartments(int performingUserId) { using (var context = new PrometheusContext()) { foreach (var department in context.Departments) { yield return(ManualMapper.MapDepartmentToDto(department)); } } }
protected override IDepartmentDto Create(int performingUserId, IDepartmentDto departmentDto) { using (var context = new PrometheusContext()) { var department = context.Departments.Find(departmentDto.Id); if (department != null) { throw new InvalidOperationException(string.Format("Department with ID {0} already exists.", departmentDto.Id)); } var savedDepartment = context.Departments.Add(ManualMapper.MapDtoToDepartment(departmentDto)); context.SaveChanges(performingUserId); return(ManualMapper.MapDepartmentToDto(savedDepartment)); } }
protected override IDepartmentDto Update(int performingUserId, IDepartmentDto departmentDto) { using (var context = new PrometheusContext()) { if (!context.Departments.Any(x => x.Id == departmentDto.Id)) { throw new InvalidOperationException(string.Format("Department with ID {0} cannot be updated since it does not exist.", departmentDto.Id)); } var updatedDepartment = ManualMapper.MapDtoToDepartment(departmentDto); context.Departments.Attach(updatedDepartment); context.Entry(updatedDepartment).State = EntityState.Modified; context.SaveChanges(performingUserId); return(ManualMapper.MapDepartmentToDto(updatedDepartment)); } }