Esempio n. 1
0
        public async Task <ActionResult <News> > Post(News news)
        {
            if (news == null)
            {
                return(BadRequest());
            }

            db.News.Add(news);
            await db.SaveChangesAsync();

            return(Ok(news));
        }
        public async Task <IActionResult> Register(RegisterModel model)
        {
            if (ModelState.IsValid)
            {
                User user = await _context.Users.FirstOrDefaultAsync(x => x.Email == model.Email);

                if (user == null)
                {
                    user = new User {
                        Email    = model.Email,
                        Password = model.Password
                    };
                    Role userRole = await _context.Roles.FirstOrDefaultAsync(x => x.Name == "user");

                    if (userRole != null)
                    {
                        user.Role = userRole;
                        _context.Users.Add(user);
                        await _context.SaveChangesAsync();
                        await Authenticate(user);

                        return(RedirectToAction("Index", "Home"));
                    }
                }
                else
                {
                    ModelState.AddModelError("", "Неверные логин или пароль");
                }
            }
            return(View(model));
        }
Esempio n. 3
0
        public async Task <IActionResult> ToBasket(int Id)
        {
            var p = await _context.Presents.FindAsync(Id);

            var basket = new Basket()
            {
                Name        = p.Name,
                PresentsId  = p.Id,
                Price       = p.Price,
                Description = p.Description
            };
            var user = await _context.Users.FirstOrDefaultAsync(x => x.Email == User.Identity.Name);

            basket.UserId = user.Id;
            _context.Baskets.Add(basket);
            await _context.SaveChangesAsync();

            ViewBag.Id = basket.Id;
            return(View(basket));
        }
        public async Task <IActionResult> Add(Present p)
        {
            //копируем картинку и записываем ее путь в бд
            string wwRootPath = _hostEnvironment.WebRootPath;
            string file       = Path.GetFileNameWithoutExtension(p.ImgFile.FileName);
            string extension  = Path.GetExtension(p.ImgFile.FileName);

            file  = file + DateTime.Now.ToString("yymmssfff") + extension;
            p.Img = file;
            var path       = Path.Combine(wwRootPath + "/img/", file);
            var fileStream = new FileStream(path, FileMode.Create);
            await p.ImgFile.CopyToAsync(fileStream);

            //добавление нового подарка в бд после получения данных
            var gender = await _context.Genders.FirstOrDefaultAsync(x => x.Id == p.GenderId);

            p.gender = gender;
            var role = await _context.RoleForUsers.FirstOrDefaultAsync(x => x.Id == p.RoleId);

            p.role = role;
            var fest = await _context.Festivals.FirstOrDefaultAsync(x => x.Id == p.FestivalId);

            p.festival = fest;
            var interest = await _context.Interests.FirstOrDefaultAsync(x => x.Id == p.InterestId);

            p.interest = interest;
            var prof = await _context.Professions.FirstOrDefaultAsync(x => x.Id == p.ProfessionId);

            p.profession = prof;
            _context.Presents.Add(p);
            gender.Presents.Add(p);
            prof.Presents.Add(p);
            interest.Presents.Add(p);
            fest.Presents.Add(p);
            role.Presents.Add(p);
            await _context.SaveChangesAsync();

            return(RedirectToAction("GetAllPresents"));
        }