public async Task<ActionResult> Post(Space space)
        {
            try
            {
                var postRepo = new PostRepository();

                switch (space.AvailabilityType)
                {
                    case "Specific Date" :
                        if (space.Availability.SpecificDate == DateTime.MinValue)
                        {
                            Response.StatusCode = (int)HttpStatusCode.InternalServerError;
                            return Json(new { success = false, responseText = "Invalid specific date." }, JsonRequestBehavior.AllowGet);
                        }
                        break;
                    case "Date Range":
                        if (space.Availability.DateFrom == DateTime.MinValue || space.Availability.DateTo == DateTime.MinValue)
                        {
                            Response.StatusCode = (int)HttpStatusCode.InternalServerError;
                            return Json(new { success = false, responseText = "Invalid date range." }, JsonRequestBehavior.AllowGet);
                        }
                        break;
                }
                space.Availability.DateFrom = DateTime.SpecifyKind(space.Availability.DateFrom, DateTimeKind.Utc);
                space.Availability.DateTo = DateTime.SpecifyKind(space.Availability.DateTo, DateTimeKind.Utc);
                space.Availability.SpecificDate = DateTime.SpecifyKind(space.Availability.SpecificDate, DateTimeKind.Utc);

                var data = new Data.Model.Space
                {
                    User = User.Identity.Name,
                    Address = space.Address,
                    latitude = space.latitude,
                    longitude = space.longitude,
                    VehicleType = space.VehicleType,
                    NumberOfSlot = space.NumberOfSlot,
                    AvailabilityType = space.AvailabilityType,
                    Availability = space.Availability,
                    Price = space.Price,
                    Instructions = space.Instructions,
                    DatePosted = DateTime.SpecifyKind(DateTime.Now, DateTimeKind.Utc)
                };

                await postRepo.CreateSync(data);

                

                var accountStatement = new AccountStatementRepository();
                var amount = await accountStatement.GetCurrentBalance(User.Identity.Name) + 10;
                var promo = new AccountStatement
                {
                    User = User.Identity.Name,
                    Credit = 10,
                    CurrentBalance = amount,
                    TransactionDate = Helper.SetDateForMongo(DateTime.Now),
                    Description = "Early adopter $10 promo"
                };

                accountStatement.Create(promo);


                return Json(new { success = true, responseText = "Added.", id = data.Id.ToString() }, JsonRequestBehavior.AllowGet);
            }
            catch (Exception ex)
            {
                Response.StatusCode = (int)HttpStatusCode.InternalServerError;
                return Json(new { success = false, responseText = ex.Message }, JsonRequestBehavior.AllowGet);
            }

        }
Example #2
0
        public async Task <ActionResult> Post(Space space)
        {
            try
            {
                var postRepo = new PostRepository();

                switch (space.AvailabilityType)
                {
                case "Specific Date":
                    if (space.Availability.SpecificDate == DateTime.MinValue)
                    {
                        Response.StatusCode = (int)HttpStatusCode.InternalServerError;
                        return(Json(new { success = false, responseText = "Invalid specific date." }, JsonRequestBehavior.AllowGet));
                    }
                    break;

                case "Date Range":
                    if (space.Availability.DateFrom == DateTime.MinValue || space.Availability.DateTo == DateTime.MinValue)
                    {
                        Response.StatusCode = (int)HttpStatusCode.InternalServerError;
                        return(Json(new { success = false, responseText = "Invalid date range." }, JsonRequestBehavior.AllowGet));
                    }
                    break;
                }
                space.Availability.DateFrom     = DateTime.SpecifyKind(space.Availability.DateFrom, DateTimeKind.Utc);
                space.Availability.DateTo       = DateTime.SpecifyKind(space.Availability.DateTo, DateTimeKind.Utc);
                space.Availability.SpecificDate = DateTime.SpecifyKind(space.Availability.SpecificDate, DateTimeKind.Utc);

                var data = new Data.Model.Space
                {
                    User             = User.Identity.Name,
                    Address          = space.Address,
                    Country          = space.Country,
                    City             = space.City,
                    latitude         = space.latitude,
                    longitude        = space.longitude,
                    VehicleType      = space.VehicleType,
                    NumberOfSlot     = space.NumberOfSlot,
                    AvailabilityType = space.AvailabilityType,
                    Availability     = space.Availability,
                    Price            = space.Price,
                    Instructions     = space.Instructions,
                    DatePosted       = DateTime.SpecifyKind(DateTime.Now, DateTimeKind.Utc)
                };

                await postRepo.CreateSync(data);



                var accountStatement = new AccountStatementRepository();
                var amount           = await accountStatement.GetCurrentBalance(User.Identity.Name) + 10;

                var promo = new AccountStatement
                {
                    User            = User.Identity.Name,
                    Credit          = 10,
                    CurrentBalance  = amount,
                    TransactionDate = Helper.SetDateForMongo(DateTime.Now),
                    Description     = "Early adopter $10 promo"
                };

                accountStatement.Create(promo);


                return(Json(new { success = true, responseText = "Added.", id = data.Id.ToString() }, JsonRequestBehavior.AllowGet));
            }
            catch (Exception ex)
            {
                Response.StatusCode = (int)HttpStatusCode.InternalServerError;
                return(Json(new { success = false, responseText = ex.Message }, JsonRequestBehavior.AllowGet));
            }
        }