Exemplo n.º 1
0
        public Result BookingSeat(int totalSeats, string seatIds, string jsonBooked, IMessage msg)
        {
            Result result;

            using (IDataAdapter da = DataManager.Build(this.DbConfig))
            {
                IClientSessionHandle Session = null;
                try
                {
                    string tickets         = "";
                    string seatUnavailable = @"
                    {
                        _id: {'$in': <ids>},
                        'bookingStatus._id': {'$ne': 'bs01'}
                    }
                    ".Replace("<ids>", seatIds);
                    Session = da.Open()
                              .CreateTransaction();
                    Session.StartTransaction();
                    da.Get(seatUnavailable, out tickets);

                    if (tickets != "[]")
                    {
                        Session.AbortTransaction();
                        throw new System.InvalidOperationException("Unavailable ticket:" + tickets);
                    }

                    string bookSeats = @"
                    {
                        '_id': {'$in': <ids>}
                    }
                    ".Replace("<ids>", seatIds);

                    long total = 0;
                    da.EditMany(bookSeats, jsonBooked, out total);
                    if (totalSeats != total)
                    {
                        Session.AbortTransaction();
                        throw new System.InvalidOperationException("Unavailable book all ticktes!");
                    }

                    Session.CommitTransaction();
                    result = Result.GetResult(BusinessStatus.Completed, 210, msg, "Booking is successfully.");
                }
                catch (Exception err)
                {
                    if (Session != null)
                    {
                        Session.AbortTransaction();
                    }
                    result = Result.GetResult(BusinessStatus.Completed, 500, msg, err.Message);
                }
            }

            return(result);
        }