public IActionResult Index(IFormFile file, [FromForm] BlogPost post) { var userIdCookie = GetEncryptedUserCookie("USER_ID"); if (userIdCookie == null) { //can't Blog without login return(RedirectToAction("Index", "Home")); } else { string filePath = null; using (var stream = file.OpenReadStream()) { var connectionString = _Configuration.GetConnectionString("StorageConnection"); filePath = AzureStorage.AddUpdateFile(file.FileName, stream, connectionString, "Team1"); } post.ImageUrl = filePath; _CoWork454Context.Add(post); _CoWork454Context.SaveChanges(); return(new JsonResult(post)); } }
public IActionResult AddBooking(MakeBooking makeBooking) { //create a booking from makebooking. Parse the date/time strings to a datetimeoffset. Booking booking = new Booking() { ProductId = makeBooking.ProductId, Date_start = DateTimeOffset.ParseExact($"{makeBooking.Date} {makeBooking.TimeStart}", "yyyy-MM-dd HH:mm", CultureInfo.InvariantCulture), Date_end = DateTimeOffset.ParseExact($"{makeBooking.Date} {makeBooking.TimeFinish}", "yyyy-MM-dd HH:mm", CultureInfo.InvariantCulture) }; var orderIdCookie = GetEncryptedUserCookie("ORDER_ID"); if (orderIdCookie == null) { // create new order var order = new Order(); // create bookings list and add to new booking order.Bookings = new List <Booking>(); order.Bookings.Add(booking); var userId = GetEncryptedUserCookie("USER_ID"); if (userId != null) { order.UserId = Convert.ToInt32(userId); } // add the order to the context, save changes to update database _CoWork454Context.Add(order); _CoWork454Context.SaveChanges(); // set the orderId in a cookie SetEncryptedUserCookie("ORDER_ID", order.Id.ToString()); // we are going to show the user var currentBookings = _CoWork454Context.Booking.Include(o => o.Order) .Where(o => o.Order.UserId == order.UserId).ToList(); ViewData["Bookings"] = currentBookings; ViewData["Products"] = _CoWork454Context.Product.ToList(); ViewData["BookingRequest"] = makeBooking; ViewData["User"] = _CoWork454Context.User .SingleOrDefault(u => u.Id == order.UserId); } else { // already has an order var orderId = Convert.ToInt32(orderIdCookie); // get the order from the database var order = _CoWork454Context.Order. Include(o => o.Bookings) .SingleOrDefault(o => o.Id == orderId); if (order.Bookings == null) { return(NotFound()); } // get the existing order item/s with the same ProductID, if any var existingBookings = _CoWork454Context.Booking .SingleOrDefault(b => b.OrderId == orderId && b.ProductId == booking.ProductId); if (existingBookings == null) { //no bookings of same product on the order so add booking order.Bookings.Add(booking); //update database _CoWork454Context.SaveChanges(); } else { //Already has booking of that product id so make new order var newOrder = new Order(); // create bookings list and add booking newOrder.Bookings = new List <Booking>(); newOrder.Bookings.Add(booking); var userId = GetEncryptedUserCookie("USER_ID"); if (userId != null) { newOrder.UserId = Convert.ToInt32(userId); } // add the order to the context, save changes to update database _CoWork454Context.Add(newOrder); _CoWork454Context.SaveChanges(); // set the orderId in a cookie SetEncryptedUserCookie("ORDER_ID", newOrder.Id.ToString()); } //update viewdata var currentBookings = _CoWork454Context.Booking.Include(o => o.Order) .Where(o => o.Order.UserId == order.UserId).ToList(); ViewData["Bookings"] = currentBookings; ViewData["Products"] = _CoWork454Context.Product.ToList(); ViewData["User"] = _CoWork454Context.User .SingleOrDefault(u => u.Id == order.UserId); } return(View("Members")); }