Exemplo n.º 1
0
        public HttpResponseMessage Post(TradeDTO trade)
        {
            if(ModelState.IsValid) {
                _service.SwapShifts(trade.ShiftForTrade, trade.ShiftAccepted);

                return Request.CreateResponse(HttpStatusCode.OK, trade);
            } else {
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, this.ModelState);
            }
        }
Exemplo n.º 2
0
 //method for adding a trade to database
 public void AddTrade(TradeDTO trade)
 {
     var newTrade = new Trade {
         ShiftForTrade = _repo.Query<Shift>().Include(s => s.Employee).FirstOrDefault(s => s.Id == trade.ShiftForTrade.Id),
         ShiftOffered = _repo.Query<Shift>().Include(s => s.Employee).FirstOrDefault(s => s.Id == trade.ShiftOffered.Id),
         ShiftsOffered = new List<Shift>(),
         IsPending = true
     };
     newTrade.ShiftsOffered.Add(newTrade.ShiftOffered);
     _repo.Add<Trade>(newTrade);
     _repo.SaveChanges();
 }
Exemplo n.º 3
0
 public HttpResponseMessage Post(TradeDTO trade)
 {
     if (ModelState.IsValid) {
         var newTrade = _service.FindTrade(trade.ShiftForTrade.Id);
         if (trade.Id == 0 && newTrade == null) {
             _service.AddTrade(trade);
             return Request.CreateResponse(HttpStatusCode.OK, trade);
         } else {
             if (!newTrade.IsPending) {
                 _service.UpdateTrade(trade);
                 return Request.CreateResponse(HttpStatusCode.OK, trade);
             } else {
                 _service.AddShiftToTrade(trade);
                 return Request.CreateResponse(HttpStatusCode.OK, trade);
             }
         }
     }
     else {
         return Request.CreateErrorResponse(HttpStatusCode.BadRequest, this.ModelState);
     }
 }
Exemplo n.º 4
0
 //method for a shift offer to an existing trade
 public void AddShiftToTrade(TradeDTO trade)
 {
     var originalTrade = _repo.Query<Trade>().Include(t => t.ShiftsOffered.Select(s => s.Employee)).FirstOrDefault(t => t.ShiftForTrade.Id == trade.ShiftForTrade.Id);
     originalTrade.ShiftsOffered.Add(_repo.Query<Shift>().Include(s => s.Employee).FirstOrDefault(s => s.Id == trade.ShiftOffered.Id));
     _repo.SaveChanges();
 }
Exemplo n.º 5
0
 //method for updating an existing trade
 public void UpdateTrade(TradeDTO trade)
 {
     var originalTrade = _repo.Query<Trade>().Include(t => t.ShiftsOffered).Include(t => t.ShiftForTrade.Employee).FirstOrDefault(t => t.ShiftForTrade.Id == trade.ShiftForTrade.Id);
     originalTrade.ShiftForTrade.Employee = _repo.Query<ApplicationUser>().FirstOrDefault(u => u.Email == trade.ShiftForTrade.Employee.Email);
     var shiftOffered = _repo.Query<Shift>().Include(s => s.Employee).FirstOrDefault(s => s.Id == trade.ShiftOffered.Id);
     originalTrade.ShiftsOffered.Add(shiftOffered);
     originalTrade.IsPending = true;
     _repo.SaveChanges();
 }
Exemplo n.º 6
0
        //get a list of pending trade
        public IList<TradeDTO> ListTrades()
        {
            var trades = _repo.Query<Trade>().Include(s => s.ShiftsOffered.Select(a => a.Employee)).Include(s => s.ShiftForTrade.Employee).ToList();
            var tradesDTO = new List<TradeDTO>();
            foreach (var t in trades) {

                var shiftList = new List<ShiftDTO>();
                foreach (var st in t.ShiftsOffered) {
                    var shift = new ShiftDTO {
                        Id = st.Id,
                        StartShift = st.StartShift,
                        EndShift = st.EndShift,
                        Employee = Mapper.Map<ApplicationUserDTO>(st.Employee),
                        IsAvailable = st.IsAvailable
                    };
                    shiftList.Add(shift);
                }
                var tradeDTO = new TradeDTO {
                    Id = t.Id,
                    ShiftForTrade = new ShiftDTO {
                        Id = t.ShiftForTrade.Id,
                        StartShift = t.ShiftForTrade.StartShift,
                        EndShift = t.ShiftForTrade.EndShift,
                        Employee = Mapper.Map<ApplicationUserDTO>(t.ShiftForTrade.Employee),
                        IsAvailable = t.ShiftForTrade.IsAvailable
                    },
                    ShiftsOffered = shiftList,
                    IsPending = t.IsPending
                };

                tradesDTO.Add(tradeDTO);
            }
            return tradesDTO;
        }