Esempio n. 1
0
        //New user requesting a new token
        public async Task <IActionResult> RequestNewToken([FromBody] Token t)
        {
            if (string.IsNullOrEmpty(t.Owner))
            {
                return(this.Problem("Missing owner"));
            }
            if (string.IsNullOrEmpty(t.Class))
            {
                return(this.Problem("Missing class"));
            }
            var token = Token.CreateRequest(t.Owner, t.Class);

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

            return(Ok(token));
        }
Esempio n. 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));
        }