Esempio n. 1
0
        public async Task <IActionResult> AddDog(AddNewDogModel model)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    var files = HttpContext.Request.Form.Files;
                    foreach (var Image in files)
                    {
                        if (Image != null && Image.Length > 0)
                        {
                            var file    = Image;
                            var uploads = Path.Combine(_appEnvironment.WebRootPath, "images");
                            if (file.Length > 0)
                            {
                                var fileName = Guid.NewGuid().ToString().Replace("-", "") + Path.GetExtension(file.FileName);
                                using (var fileStream = new FileStream(Path.Combine(uploads, fileName), FileMode.Create))
                                {
                                    await file.CopyToAsync(fileStream);

                                    var newDog = new Dog(model.Name, model.Race, model.BirthDate, fileName);
                                    newDog.AddSize(model.Size);
                                    newDog.SetGender(model.Gender);
                                    newDog.AddHairLenght(model.Lenght);
                                    _repository.AddDog(newDog);
                                    return(RedirectToAction("Home"));
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                _repository.AddError(ex.Message);
                return(View("Error"));
            }
            return(View(model));
        }