Esempio n. 1
0
        public async Task <IActionResult> Create(HomeCreateViewsModel model)
        {
            if (ModelState.IsValid)
            {
                var emp = new CreateEmployeeViewsModel()
                {
                    Name         = model.Name,
                    Price        = model.Price,
                    CarbrandId   = model.CarbrandId,
                    Descriptions = model.Descriptions,
                    Color        = model.Color,
                    Register     = model.Register,
                    Yeard        = model.Yeard
                };
                if (model.GalleryFiles != null)
                {
                    string folder = "gallery/";

                    model.Gallery = new List <GalleryModel>();

                    foreach (var file in model.GalleryFiles)
                    {
                        var gallery = new GalleryModel()
                        {
                            Name = file.FileName,
                            Url  = await UploadImage(folder, file)
                        };
                        model.Gallery.Add(gallery);
                    }
                    emp.Gallery = model.Gallery;
                    /////emp.Gallery = model.Gallery;
                    if (model.Avatar != null)
                    {
                        string routes   = Path.Combine(webHostEnvironment.WebRootPath, "images");
                        var    filename = $"{Guid.NewGuid()}_{model.Avatar.FileName}";
                        filename = model.Avatar.FileName;
                        var load = Path.Combine(routes, filename);
                        using (var stream = new FileStream(load, FileMode.Create))
                        {
                            model.Avatar.CopyTo(stream);
                        }
                    }
                    ;

                    emp.AvatarPath = model.Avatar.FileName;
                    var hala = employeesRepository.Create(emp);
                    if (hala != null)
                    {
                        return(RedirectToAction("table", new { id = hala.EmployId }));
                    }
                }
            }
            return(View());
        }
        public CreateEmployeeViewsModel Create(CreateEmployeeViewsModel employees)
        {
            ///Craete new Employyee
            var empl = new Employees()
            {
                Name       = employees.Name,
                AvatarPath = employees.AvatarPath,
                CarbrandId = employees.CarbrandId,
                Price      = employees.Price,
                Color      = employees.Color,
                Yeard      = employees.Yeard,
                Register   = employees.Register
            };

            if (employees.Gallery != null)
            {
                empl.Gallerys = new List <Gallery>();
                foreach (var file in employees.Gallery)
                {
                    empl.Gallerys.Add(new Gallery()
                    {
                        Name = file.Name,
                        Url  = file.Url
                    });
                }
            }
            context.Employees.AddRange(empl);
            context.SaveChanges();

            ///Check if Description !=null create new DescriptionEmployee
            if (employees.Descriptions != null)
            {
                IEnumerable <EmployeeDescription> elist = employees.Descriptions.Select(a => new EmployeeDescription()
                {
                    DescriptionId = a,
                    EmployeeId    = empl.Id
                }).ToList();
                context.EmployeeDescriptions.AddRange(elist);
                context.SaveChanges();
            }
            employees.EmployId = empl.Id;

            return(employees);
        }