Esempio n. 1
0
        public IActionResult Post(Booking booking)
        {
            try
            {
                if (!Request.Headers.ContainsKey("access_token"))
                {
                    return(Unauthorized());
                }

                string token       = Request.Headers["access_token"];
                var    authManager = new AuthorizationManager();
                var    tokenStatus = authManager.CheckToken(token);
                if (tokenStatus == TokenStatusesEnum.Invalid)
                {
                    return(Json(new { error = "Invalid token", error_description = "Given token is invalid" }));
                }
                else if (tokenStatus == TokenStatusesEnum.Expired)
                {
                    return(Json(new { error = "Expired token", error_description = "Given token is expired. Please refresh it." }));
                }

                //var booking = JsonConvert.DeserializeObject<Booking>(bookingStr);
                booking.InitId();
                var savedBooking = new BookingManager().AddBooking(booking);
                var bill         = new BillingManager().AddBill(booking.DaysCount * 1500, "OOO Sberbank 91929393234923", savedBooking.Id);
                savedBooking.Bill = bill;
                return(Json(savedBooking));
            }
            catch (Exception e)
            {
                return(Json(new { error = "Internal server error", error_description = JsonConvert.SerializeObject(e, Formatting.Indented) }));
            }
        }
        public IActionResult Delete(Guid billId)
        {
            try
            {
                if (!Request.Headers.ContainsKey("access_token"))
                {
                    return(Unauthorized());
                }

                string token       = Request.Headers["access_token"];
                var    authManager = new AuthorizationManager();
                var    tokenStatus = authManager.CheckToken(token);
                if (tokenStatus == TokenStatusesEnum.Invalid)
                {
                    return(Json(new { error = "Invalid token", error_description = "Given token is invalid" }));
                }
                else if (tokenStatus == TokenStatusesEnum.Expired)
                {
                    return(Json(new { error = "Expired token", error_description = "Given token is expired. Please refresh it." }));
                }

                var billManager = new BillingManager();
                return(Json(new BillingManager().RemoveBill(billId)));
            }
            catch (Exception e)
            {
                return(Json(new { error = "Internal server error", error_description = JsonConvert.SerializeObject(e, Formatting.Indented) }));
            }
        }
Esempio n. 3
0
        public IActionResult Post(Booking booking)
        {
            try
            {
                if (!Request.Headers.ContainsKey("access_token"))
                {
                    return(Unauthorized());
                }

                string token       = Request.Headers["access_token"];
                var    authManager = new AuthorizationManager();
                var    tokenStatus = authManager.CheckToken(token);
                if (tokenStatus == TokenStatusesEnum.Invalid)
                {
                    return(Json(new { error = "Invalid token", error_description = "Given token is invalid" }));
                }
                else if (tokenStatus == TokenStatusesEnum.Expired)
                {
                    return(Json(new { error = "Expired token", error_description = "Given token is expired. Please refresh it." }));
                }

                booking.InitId();
                var  savedBooking = RESTRequest.PostAsJsonWithJsonResponse <Booking>(appSettings.BookingServerUri + "api/bookings", booking);
                Bill bill         = null;
                try
                {
                    bill = RESTRequest.PostAsJsonWithJsonResponse <Bill>(appSettings.BillingServerUri + "api/bills", new Bill()
                    {
                        Price      = booking.DaysCount * 1500,
                        Requisites = "OOO Sberbank 91929393234923",
                        BookingId  = savedBooking.Id
                    });
                    if (bill == null)
                    {
                        throw new JsonException("Bill remote saving failed");
                    }
                }
                catch (Exception ex)
                {
                    RESTRequest.DeleteWithJsonResponse <Booking>(appSettings.BookingServerUri + $"api/bookings/{savedBooking.Id}");
                    throw;
                }
                savedBooking.Bill = bill;
                return(Json(savedBooking));
            }
            catch (Exception e)
            {
                return(Json(new { error = "Internal server error", error_description = JsonConvert.SerializeObject(e, Formatting.Indented) }));
            }
        }
Esempio n. 4
0
        public IActionResult Get(string userLogin)
        {
            try
            {
                if (!Request.Headers.ContainsKey("access_token"))
                {
                    return(Unauthorized());
                }

                string token       = Request.Headers["access_token"];
                var    authManager = new AuthorizationManager();
                if (!authManager.CheckTokenBelonging(token, userLogin))
                {
                    return(Json(new { error = "Invalid token", error_description = "Given token doesn't belong to this user" }));
                }
                var tokenStatus = authManager.CheckToken(token);
                if (tokenStatus == TokenStatusesEnum.Invalid)
                {
                    return(Json(new { error = "Invalid token", error_description = "Given token is invalid" }));
                }
                else if (tokenStatus == TokenStatusesEnum.Expired)
                {
                    return(Json(new { error = "Expired token", error_description = "Given token is expired. Please refresh it." }));
                }

                var bookings    = new BookingManager().GetUserBookings(userLogin);
                var billManager = new BillingManager();
                foreach (var booking in bookings)
                {
                    booking.Bill = billManager.GetBill(booking.Id);
                }

                return(Json(bookings));
            }
            catch (Exception e)
            {
                return(Json(new { error = "Internal server error", error_description = JsonConvert.SerializeObject(e, Formatting.Indented) }));
            }
        }