Exemple #1
0
        public async Task <IActionResult> AddDog(Dog dog, IFormFile formFile)
        {
            if (ModelState.IsValid)
            {
                if (formFile != null)
                {
                    string path = "/Avatars/" + formFile.FileName;

                    using (var fileStream = new FileStream(_appEnvironment.WebRootPath + path, FileMode.Create))
                    {
                        await formFile.CopyToAsync(fileStream);
                    }

                    dog.Avatar     = formFile.FileName;
                    dog.AvatarPath = path;
                }

                _context.Dogs.Add(dog);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Dogs)));
            }

            return(View(dog));
        }
Exemple #2
0
        public async Task <IActionResult> Register(RegisterViewModel viewModel)
        {
            if (ModelState.IsValid)
            {
                User user = await _context
                            .Users.FirstOrDefaultAsync(u => u.Email == viewModel.Email);

                DogHandler dogHandler = new DogHandler
                {
                    Name     = viewModel.Name,
                    Surname  = viewModel.Surname,
                    Position = viewModel.Position
                };

                if (user == null)
                {
                    user = new User {
                        Email      = viewModel.Email,
                        Password   = viewModel.Password,
                        DogHandler = dogHandler
                    };

                    Role userRole = await _context.Roles.FirstOrDefaultAsync(r => r.Title == "Кінолог");

                    if (userRole != null)
                    {
                        user.Role = userRole;
                    }

                    _context.Users.Add(user);
                    _context.DogHandlers.Add(dogHandler);

                    await _context.SaveChangesAsync();

                    await Authenticate(user);

                    if (user.Role.Title == "Адміністратор")
                    {
                        return(RedirectToAction("Index", "Admin"));
                    }
                    else
                    {
                        return(RedirectToAction("Index", "DogHandler"));
                    }
                }
                else
                {
                    ModelState.AddModelError("", "Некоректний логін чи(та) пароль.");
                }
            }
            return(View(viewModel));
        }