protected void DataPortal_Fetch(EmployeeSearchCriteria criteria)
        {
            RaiseListChangedEvents = false;
            IsReadOnly             = false;

            using (var ctx = new VacationManagerContext())
            {
                IList <Persistence.Model.EmployeeEntity> employees;

                if (criteria == null)
                {
                    employees = ctx.Employees
                                .ToList();
                }
                else
                {
                    employees = ctx.Employees
                                .Where(x => (x.Firstname == criteria.FirstName) ||
                                       (x.LastName == criteria.LastName))
                                .ToList();
                }

                foreach (var item in employees)
                {
                    Add(DataPortal.Create <Employee>(item));
                }
            }

            IsReadOnly             = true;
            RaiseListChangedEvents = true;
        }
        protected void DataPortal_Fetch(UsernameCriteria criteria)
        {
            using (var ctx = new VacationManagerContext())
            {
                var employee = ctx.Employees
                               .SingleOrDefault(x => x.Email == criteria.Username);

                LoadIdentityFrom(employee);
            }
        }
        protected void DataPortal_Fetch(long id)
        {
            using (var ctx = new VacationManagerContext())
            {
                var employee = ctx.Employees.Find(id);

                EmployeeId = employee.Id;
                LastName   = employee.LastName;
                FirstName  = employee.Firstname;
                Roles      = employee.Roles;
            }
        }
        protected void DataPortal_Fetch(long employeeId)
        {
            using (var ctx = new VacationManagerContext())
            {
                var vacationDays = ctx.Situations
                                   .FirstOrDefault(x => x.Employee.Id == employeeId);

                // TODO: what if vacationDays is null?

                _consumedDays  = vacationDays.ConsumedDays;
                _availableDays = vacationDays.AvailableDays;
            }
        }
        public void AddUserToVMContext(InputModel user)
        {
            VacationManagerContext _context = new VacationManagerContext();

            _context.Users.Add(new User
            {
                UserName  = user.Email,
                Password  = user.Password,
                FirstName = Input.FirstName,
                LastName  = Input.LirstName
            });
            _context.SaveChanges();
        }
Example #6
0
        protected override void DataPortal_Execute()
        {
            using (var ctx = new VacationManagerContext())
            {
                var request = ctx.Requests.FirstOrDefault(x => x.Id == _requestNumber);
                if (request == null)
                {
                    throw new ApplicationException(string.Format(
                                                       "Request number {0} was not found. It must exist in order to change it's state.", _requestNumber));
                }

                request.State = _state;
                ctx.SaveChanges();
            }
        }
        protected void DataPortal_Fetch(VacationRequestSearchCriteria criteria)
        {
            RaiseListChangedEvents = false;
            IsReadOnly             = false;

            using (var ctx = new VacationManagerContext())
            {
                IList <Persistence.Model.VacationRequestEntity> requests;

                if (criteria == null)
                {
                    requests = ctx.Requests.ToList();
                }
                else
                {
                    var query = ctx.Requests.AsQueryable();
                    if (criteria.EmployeeId != 0)
                    {
                        if (criteria.GetMine)
                        {
                            query = query.Where(x => x.Employee.Id == criteria.EmployeeId);
                        }
                        else
                        {
                            query = query.Where(x => x.Employee.Manager.Id == criteria.EmployeeId);
                        }
                    }

                    if (criteria.States != null)
                    {
                        query = query.Where(x => criteria.States.Contains(x.State));
                    }

                    requests = query.OrderByDescending(x => x.CreationDate)
                               .ToList();
                }

                foreach (var item in requests)
                {
                    Add(DataPortal.Create <VacationRequest>(item));
                }
            }

            IsReadOnly             = true;
            RaiseListChangedEvents = true;
        }
Example #8
0
        /// <summary>
        /// Remove service object having parameter id. For the moment we assume this
        /// means to cancel a submitted request.
        /// </summary>
        protected void DataPortal_Delete(long id)
        {
            using (var ctx = new VacationManagerContext())
            {
                var request = ctx.Requests.FirstOrDefault(x => x.Id == id);
                if (request == null)
                {
                    throw new ApplicationException(string.Format(
                                                       "Request number {0} was not found. It must exist in order to be deleted.", id));
                }

                if (request.State != VacationRequestState.Submitted)
                {
                    throw new ApplicationException(string.Format(
                                                       "Request {0} was already {1}, cannot be deleted anymore. It must be in submited state in order to be deleted.",
                                                       id, request.State));
                }

                ctx.Requests.Remove(request);
                ctx.SaveChanges();
            }
        }
Example #9
0
        /// <summary>
        /// Create new service object and persist it. For the moment we assume this
        /// means to submit a new vacation request.
        /// </summary>
        protected override void DataPortal_Insert()
        {
            using (var ctx = new VacationManagerContext())
            {
                var employee = ctx.Employees.FirstOrDefault(x => x.Id == EmployeeId);
                if (employee == null)
                {
                    throw new ApplicationException(
                              string.Format("New request is associated with inexistent employee having id {0}.", EmployeeId));
                }

                var numberOfVacationDaysLeft = ctx.Situations.Single(x => x.Employee.Id == EmployeeId).AvailableDays;
                if ((NumberOfDays < 1) || (NumberOfDays > numberOfVacationDaysLeft))
                {
                    throw new ApplicationException(
                              string.Format("New request has invalid number of vacation days {0}. Must greather than 0 and less or equal than days left {1}.", NumberOfDays, NumberOfDays));
                }

                var request = new Persistence.Model.VacationRequestEntity
                {
                    CreationDate = DateTime.Now, // TODO: utc?
                    StartDate    = StartDate,
                    EndDate      = EndDate,
                    State        = VacationRequestState.Submitted,
                    Employee     = employee,
                };

                ctx.Requests.Add(request);
                ctx.SaveChanges();

                _requestNumber    = request.Id;
                _submissionDate   = request.CreationDate;
                _stateId          = request.State;
                _employeeFullName = employee.Firstname + " " + employee.LastName;
            }
        }
 public void TestInitialization()
 {
     _ctx = new VacationManagerContext();
 }
 public UserController()
 {
     _context = new VacationManagerContext();
 }
 public void TestInitialization()
 {
     _ctx = new VacationManagerContext();
 }