Ejemplo n.º 1
0
 public static object SerializeReplacementRequest(this ReplacementRequest request)
 {
     if (request == default(ReplacementRequest))
     {
         return new { }
     }
     ;
     return(new
     {
         id = request.Id,
         shiftId = request.Shift.Id,
         userId = request.UserId,
         date = request.Date?.ToString(DateFormat)
     });
 }
Ejemplo n.º 2
0
        private ActionResult DeleteReplacementRequest(ReplacementRequest entry)
        {
            // check that user is logged in
            var user = GetCurrentUser();

            if (user == default(User))
            {
                return(401.ErrorStatusCode(Constants.Unauthorized.ToDict()));
            }

            if (entry != default(ReplacementRequest))
            {
                _context.Remove(entry);
                _context.SaveChanges();
                return(200.SuccessStatusCode(Constants.OK.ToDict()));
            }
            return(404.ErrorStatusCode(Constants.ReplacementRequestNotFound.ToDict()));
        }
Ejemplo n.º 3
0
        public static JsonResult ToJson(this ReplacementRequest request, int statusCode = 200)
        {
            var user  = SerializeUser(request.User);
            var shift = SerializeShift(request.Shift);

            var json = new JsonResult(new
            {
                id      = request.Id,
                shiftId = request.Shift.Id,
                userId  = request.UserId,
                date    = request.Date?.ToString(DateFormat),
                user,
                shift
            });

            json.StatusCode = statusCode;
            return(json);
        }
Ejemplo n.º 4
0
        private ActionResult RequestReplacement(RequestReplacementViewModel model)
        {
            // check that user is logged in
            var user = GetCurrentUser();

            if (user == default(User))
            {
                return(401.ErrorStatusCode(Constants.Unauthorized.ToDict()));
            }

            _context.Shift.Include(s => s.User).Load();
            var shift = _context.Shift.FirstOrDefault(s => s.Id == model.ShiftId);

            // check that shift exists and is repleceable
            if (shift == default(Shift))
            {
                return(404.ErrorStatusCode(Constants.ShiftNotFound.ToDict()));
            }

            if (!shift.IsRepleceable)
            {
                return(400.ErrorStatusCode(new Dictionary <string, string>
                {
                    { "SetReplaceable", "The specified shift is not replaceable." }
                }));
            }

            // check that user in not trying to replace their own shift
            if (user.UserName == shift.UserId)
            {
                return
                    (400.ErrorStatusCode(new Dictionary <string, string>()
                {
                    { "shiftId", "User cannot replace their own shift." }
                }));
            }

            // is date set?
            DateTime?date = null;

            // check date
            if (model.Date != null)
            {
                if (!model.Date.ValidateDate())
                {
                    return(400.ErrorStatusCode(new Dictionary <string, string>()
                    {
                        { "date", "Invalid date" }
                    }));
                }

                date = DateTime.Parse(model.Date);

                // check if date in past
                if (date < DateTime.Today)
                {
                    return
                        (400.ErrorStatusCode(new Dictionary <string, string>()
                    {
                        { "date", "Unable to create shift for past dates." }
                    }));
                }


                // check that date belongs to user's shift
                var ownShift = _context.Shift.FirstOrDefault(s => s.Date == date.Value);
                if (ownShift == default(Shift))
                {
                    return
                        (400.ErrorStatusCode(new Dictionary <string, string>()
                    {
                        {
                            "date",
                            "Unable to request replacement becaause user has no shift scheduled on specified date."
                        }
                    }));
                }
            }


            // make sure this request does not already exist
            _context.ReplacementRequest.Include(r => r.Shift).Load();
            var request = date != null
                ? _context.ReplacementRequest.FirstOrDefault(
                r => r.ShiftId == shift.Id && r.UserId == user.Id && r.Date != null && r.Date.Value.Date == date.Value.Date)
                : _context.ReplacementRequest.FirstOrDefault(
                r => r.ShiftId == shift.Id && r.UserId == user.Id && r.Date == null);

            if (request != default(ReplacementRequest))
            {
                return
                    (400.ErrorStatusCode(new Dictionary <string, string>()
                {
                    { "shiftId", "Identical request for this shift from this user aready exists." }
                }));
            }

            // create a new request
            request = new ReplacementRequest()
            {
                Date    = date,
                Shift   = shift,
                ShiftId = shift.Id,
                UserId  = user.Id,
                User    = user
            };
            _context.Add(request);
            _context.SaveChanges();
            return(request.ToJson(201));
        }