public async Task <HttpResponseMessage> GetNotification(
            [HttpTrigger(AuthorizationLevel.Function, "get", Route = "notification")] HttpRequest req)
        {
            string token = req.Headers["Authorization"].ToString().Replace("Bearer ", "");

            if (_authorizationsService.IsTokenValid(token, false))
            {
                Deliverer d = await _deliverersService.GetDelivererByToken(token);

                if (d == null)
                {
                    return(new HttpResponseMessage(HttpStatusCode.Unauthorized));
                }
                Notification n = await _notificationsService.GetFirstOpenNotificationByDelivererId(d.Id);

                return(n != null
                    ? new HttpResponseMessage(HttpStatusCode.OK)
                {
                    Content = new StringContent(JsonConvert.SerializeObject(n, new JsonSerializerSettings()
                    {       // Ignore loops in properties of the class
                        ReferenceLoopHandling = ReferenceLoopHandling.Ignore
                    }), Encoding.UTF8, "application/json")
                } : new HttpResponseMessage(HttpStatusCode.NoContent));
            }
            // Authorized access only
            _logger.Warning($"GetNotification was invoked by an unauthorized entity.\n");
            return(new HttpResponseMessage(HttpStatusCode.Unauthorized));
        }
        public async Task <HttpResponseMessage> GetAllAvailabilities(
            [HttpTrigger(AuthorizationLevel.Function, "get", Route = "Availabilities")] HttpRequest req)
        {
            string token = req.Headers["Authorization"].ToString().Replace("Bearer ", "");

            if (_authorizationsService.IsTokenValid(token, false))
            {
                Deliverer deliverer = await _deliverersService.GetDelivererByToken(token);

                if (deliverer == null)
                {
                    return(new HttpResponseMessage(HttpStatusCode.Unauthorized));
                }
                List <Availability> availabilities = await _availabilitiesService.GetAllAvailabilitiesOfDeliverer(deliverer.Id);

                return(availabilities != null
                    ? new HttpResponseMessage(HttpStatusCode.OK)
                {
                    Content = new StringContent(JsonConvert.SerializeObject(availabilities), Encoding.UTF8, "application/json")
                }
                    : new HttpResponseMessage(HttpStatusCode.BadRequest));
            }
            // Authorized access only
            return(new HttpResponseMessage(HttpStatusCode.Unauthorized));
        }
        public async Task <HttpResponseMessage> GetDeliveriesForUser(
            [HttpTrigger(AuthorizationLevel.Function, "get", Route = "Deliveries")] HttpRequest req)
        {
            string token = req.Headers["Authorization"].ToString().Replace("Bearer ", "");

            if (_authorizationsService.IsTokenValid(token, false))
            {
                Deliverer deliverer = await _deliverersService.GetDelivererByToken(token);

                if (deliverer == null)
                {
                    return(new HttpResponseMessage(HttpStatusCode.Unauthorized));
                }
                List <Delivery> result = await _deliveriesService.GetDeliveriesForUser(deliverer.Id);

                return(result != null
                    ? new HttpResponseMessage(HttpStatusCode.OK)
                {
                    Content = new StringContent(JsonConvert.SerializeObject(result, new JsonSerializerSettings()
                    {
                        ReferenceLoopHandling = ReferenceLoopHandling.Ignore
                    }), Encoding.UTF8, "application/json")
                }
                    : new HttpResponseMessage(HttpStatusCode.NoContent));
            }
            // Authorized access only
            return(new HttpResponseMessage(HttpStatusCode.Unauthorized));
        }
        public async Task <HttpResponseMessage> GetAllWarehouses(
            [HttpTrigger(AuthorizationLevel.Function, "get", Route = "Locations/Warehouses")] HttpRequest req)
        {
            string token = req.Headers["Authorization"].ToString().Replace("Bearer ", "");

            if (_authorizationsService.IsTokenValid(token, false))
            {
                Deliverer d = await _deliverersService.GetDelivererByToken(token);

                if (d == null)
                {
                    return(new HttpResponseMessage(HttpStatusCode.Unauthorized));
                }
                List <Location> warehouses = await _locationsService.GetAllWarehouses();

                return(warehouses != null && warehouses.Count > 0
                    ? new HttpResponseMessage(HttpStatusCode.OK)
                {
                    Content = new StringContent(JsonConvert.SerializeObject(warehouses), Encoding.UTF8, "application/json")
                } : new HttpResponseMessage(HttpStatusCode.NoContent));
            }
            // Authorized access only
            _logger.Warning($"GetAllWarehouses was invoked by an unauthorized entity.\n");
            return(new HttpResponseMessage(HttpStatusCode.Unauthorized));
        }