Exemple #1
0
        public async Task <IActionResult> productAdd(ProductCheck check)
        {
            int?id = HttpContext.Session.GetInt32("userId");

            if (id == null)
            {
                return(RedirectToAction("LoginPage", "User"));
            }
            else
            {
                if (ModelState.IsValid)
                {
                    User    exists     = _context.Users.Where(u => u.UserId == id).SingleOrDefault();
                    Product newProduct = new Product
                    {
                        Title       = check.Title,
                        Description = check.Description,
                        Price       = check.Price,
                        UserId      = (int)id,
                        CreatedAt   = DateTime.Now,
                        Condition   = check.Condition,
                        Status      = "Active"
                    };
                    var uploadDestination = Path.Combine(_hostingEnvironment.WebRootPath, "uploaded_images");
                    if (check.Image != null)
                    {
                        var filepath = Path.Combine(uploadDestination, check.Image.FileName);
                        using (var fileStream = new FileStream(filepath, FileMode.Create))
                        {
                            await check.Image.CopyToAsync(fileStream);

                            newProduct.Picture = "/uploaded_images/" + check.Image.FileName;
                        }
                    }
                    _context.Add(newProduct);
                    _context.SaveChanges();
                }
                else
                {
                    TempData["error"] = "Not added. Error";
                }
                return(RedirectToAction("Homepage"));
            }
        }
Exemple #2
0
        public IActionResult Register(Register regUser)
        {
            if (ModelState.IsValid)
            {
                User exists = _context.Users.SingleOrDefault(user => user.Email == regUser.Email);
                if (exists != null)
                {
                    ViewBag.error = "ERRRO exists";
                    ModelState.AddModelError("Email", "An account with this email already exists!");
                    return(View("Register"));
                }
                else
                {
                    PasswordHasher <Register> Hasher = new PasswordHasher <Register>();
                    string hashed  = Hasher.HashPassword(regUser, regUser.Password);
                    User   newUser = new User
                    {
                        FirstName = regUser.FirstName,
                        LastName  = regUser.LastName,
                        Email     = regUser.Email,
                        Password  = hashed,
                        Status    = "User",
                    };
                    _context.Add(newUser);
                    _context.SaveChanges();
                    User user = _context.Users.Where(u => u.Email == regUser.Email).SingleOrDefault();
                    HttpContext.Session.SetInt32("userId", user.UserId);
                    HttpContext.Session.SetString("user", user.FirstName);
                    user.ReviewedId = user.UserId;
                    _context.SaveChanges();

                    if (user.Status == "Admin")
                    {
                        return(RedirectToAction("HomepageAdmin", "Home"));
                    }
                    else
                    {
                        return(RedirectToAction("Homepage", "Home"));
                    }
                }
            }
            else
            {
                ViewBag.error = "ERROR";
                return(View("Register"));
            }
        }