示例#1
0
 public async Task <IActionResult> GetAll(string requestToken)
 {
     if (!_context.ValidateUserToken(requestToken))
     {
         return(this.Unauthorized());
     }
     return(Ok(_context.Tokens.ToList()));
 }
示例#2
0
        public async Task <IActionResult> ReservePort(string requestToken, [FromBody] Reservation requestReservation)
        {
            if (!_context.ValidateUserToken(requestToken))
            {
                return(Unauthorized());
            }
            Random      rand      = new Random();
            Reservation existing  = null;
            int         handbrake = 0;

            while (handbrake == 0 || existing != null && handbrake < 1000)
            {
                ushort unreserved = (ushort)rand.Next(1024, ushort.MaxValue);

                existing = _context.GetReservation((ushort)unreserved);
                handbrake++;
            }

            //Validate the requested reservation
            if (requestReservation.ReservedFor == null)
            {
                requestReservation.ReservedFor = _context.GetToken(requestReservation.Token);
            }
            if (requestReservation.ReservedFor == null)
            {
                return(Problem("The user this reservation is for does not exist"));
            }

            //Save reservation
            requestReservation.ApprovedBy = _context.GetToken(requestToken);
            requestReservation.Reserved   = DateTime.Now;

            _context.Add(requestReservation);
            await _context.SaveChangesAsync();

            requestReservation.ApprovedBy = null;

            //Return reservation as confirmation
            return(Ok(requestReservation));
        }