Esempio n. 1
0
 public IActionResult CreateBlogPost(BlogPostDto.List model, IFormFile photo)
 {
     try
     {
         string uniqueFileName = null;
         if (photo != null)
         {
             string uploadsFolder = Path.Combine(_env.WebRootPath, "images");
             uniqueFileName = Guid.NewGuid().ToString() + "_" + photo.FileName;
             string filePath = Path.Combine(uploadsFolder, uniqueFileName);
             using (var fileStream = new FileStream(filePath, FileMode.Create))
             {
                 photo.CopyTo(fileStream);
             }
         }
         string userId   = User.Claims.Where(c => c.Type == "UserId").FirstOrDefault().Value;
         var    blogPost = new BlogPost
         {
             BlogTitle   = model.BlogTitle,
             BlogContent = model.BlogContent,
             Photo       = uniqueFileName,
             UserId      = Int32.Parse(userId),
         };
         _context.BlogPost.Add(blogPost);
         _context.SaveChanges();
         return(RedirectToAction("List"));
     }
     catch
     {
         return(View());
     }
 }
Esempio n. 2
0
        public IActionResult DeleteAnimal(int id)
        {
            var animal = _context.Animal.Where(a => a.Id == id).FirstOrDefault();

            _context.Animal.Remove(animal);
            _context.SaveChanges();
            return(RedirectToAction("ManageAnimal"));
        }
Esempio n. 3
0
 public ActionResult Create(AnimalDto animalModel, IFormFile photo)
 {
     try
     {
         string uniqueFileName = null;
         if (photo != null)
         {
             string uploadsFolder = Path.Combine(_env.WebRootPath, "images");
             uniqueFileName = Guid.NewGuid().ToString() + "_" + photo.FileName;
             string filePath = Path.Combine(uploadsFolder, uniqueFileName);
             using (var fileStream = new FileStream(filePath, FileMode.Create))
             {
                 photo.CopyTo(fileStream);
             }
         }
         string userId = User.Claims.Where(c => c.Type == "UserId").FirstOrDefault().Value;
         var    animal = new Animal
         {
             Name        = animalModel.Name,
             Age         = animalModel.Age,
             Description = animalModel.Description,
             Photo       = uniqueFileName,
             BreedId     = animalModel.BreedId,
             UserId      = Int32.Parse(userId),
         };
         _context.Animal.Add(animal);
         _context.SaveChanges();
         return(RedirectToAction("List"));
     }
     catch
     {
         return(View());
     }
 }
Esempio n. 4
0
        public async Task <IActionResult> Register(UserDto.Register model, IFormFile photo)
        {
            string uniqueFileName = null;

            if (photo != null)
            {
                string uploadsFolder = Path.Combine(_env.WebRootPath, "images");
                uniqueFileName = Guid.NewGuid().ToString() + "_" + photo.FileName;
                string filePath = Path.Combine(uploadsFolder, uniqueFileName);
                using (var fileStream = new FileStream(filePath, FileMode.Create))
                {
                    photo.CopyTo(fileStream);
                }
            }

            var user = new User
            {
                Username  = model.Username,
                Password  = model.Password,
                FirstName = model.FirstName,
                LastName  = model.LastName,
                BirthDate = model.BirthDate,
                City      = model.City,
                Photo     = uniqueFileName,
                Phone     = model.Phone,
                RoleId    = 1,
            };

            _context.User.Add(user);
            _context.SaveChanges();
            return(RedirectToAction("Login", new UserDto.Login
            {
                Username = model.Username,
                Password = model.Password
            }));
        }