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 Register(Register model)
        {
            //only available if logged in
            var userIdCookie = GetEncryptedUserCookie("USER_ID");

            if (userIdCookie != null)
            {
                var LogginUser = _CoWork454Context.User.SingleOrDefault(l => l.Id == Convert.ToInt32(userIdCookie));
                ViewData["User"] = LogginUser;
                return(View("Members"));
            }

            // check if email already exists in database (if so throw exception)
            var existingUser = _CoWork454Context.User.SingleOrDefault(u => u.Email == model.Email);

            if (existingUser != null)
            {
                ModelState.AddModelError("Email", "Email already in use");
                //return View();
                return(RedirectToAction("Index", "Home"));
            }

            // hash the incoming password
            string passwordHash = BCrypt.Net.BCrypt.HashPassword(model.Password);

            // create a new user record
            var User = new User();

            User.FirstName    = model.FirstName;
            User.LastName     = model.LastName;
            User.Email        = model.Email;
            User.CompanyName  = model.CompanyName;
            User.Phone        = model.Phone;
            User.PasswordHash = passwordHash;


            try
            {
                _CoWork454Context.User.Add(User);
                _CoWork454Context.SaveChanges();
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("", ex.Message);
                //return View();
                return(RedirectToAction("Index", "Home"));
            }

            // redirect to the login
            return(RedirectToAction("Index", "Home"));
        }
Esempio n. 3
0
        public IActionResult Index(MailingList mail)
        {
            var existingEmail = _CoWork454Context.MailingList.SingleOrDefault(m => m.Email == mail.Email);

            if (existingEmail == null)
            {
                _CoWork454Context.MailingList.Add(mail);
                _CoWork454Context.SaveChanges();

                ViewData["Subscribe"] = "Thank you for subscribing.";
            }
            else
            {
                ViewData["Subscribe"] = "You've already subscribed. Thanks again";
            }
            return(PartialView("_SubscribeConfirm"));
        }
        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"));
        }