예제 #1
0
        public ActionResult CreateCategory(CreateComponentViewModel model)
        {
            int    parentId;
            string selectedCategory = model.SelectedCategory;

            if (!(selectedCategory == null || selectedCategory == "" || selectedCategory == "Ingen"))
            {
                Int32.TryParse(selectedCategory, out parentId);
                var component = service.FindCategoryById(parentId);
                service.AddCategoryToEvent(model.EventId, model.Title, model.Description, component);
            }
            else
            {
                service.AddCategoryToEvent(model.EventId, model.Title, model.Description, null);
            }


            return(RedirectToAction("Details", new { id = model.EventId }));
        }
예제 #2
0
        public ActionResult CreateCategory(DetailsEventViewModel model)
        {
            User u = (User)Session["User"];

            if (u == null)
            {
                return(RedirectToAction("LogIn", "User"));
            }
            int id   = model.Id;
            var evnt = service.FindEventById(model.Id);

            CreateComponentViewModel viewModel = new CreateComponentViewModel
            {
                EventId     = id,
                Title       = "",
                Description = ""
            };

            List <SelectListItem> categories = new List <SelectListItem>();

            categories.Add(new SelectListItem {
                Text = "Ingen", Value = null
            });

            foreach (var item in evnt.Components)
            {
                if (item is Category && item.Parent == null)
                {
                    categories.Add(new SelectListItem {
                        Text = item.Title, Value = item.Id.ToString()
                    });
                }
            }

            viewModel.Categories = categories;

            return(View(viewModel));
        }
예제 #3
0
        // GET: Components/Create
        public IActionResult Create()
        {
            var viewModel = new CreateComponentViewModel();

            return(View(viewModel));
        }
예제 #4
0
        public async Task <IActionResult> Create(CreateComponentViewModel model)
        {
            if (ModelState.IsValid)
            {
                var componentSaveName = Regex.Replace(model.Name, @"\s+", "");
                var component         = new Component
                {
                    Name             = model.Name,
                    Description      = model.Description,
                    ExtraDescription = model.ExtraDescription,
                    InStock          = model.InStock,
                    Used             = model.Used,
                    NormalizedString = componentSaveName
                };

                if (model.DataSheet != string.Empty)
                {
                    var path = Path.Combine(uploads, componentSaveName + "-datasheet.pdf");
                    component.DataSheet = componentSaveName + "-datasheet.pdf";
                    if (model.DataSheet.StartsWith("http"))
                    {
                        if (model.DataSheet.Contains("www.google."))
                        {
                            model.DataSheet = model.DataSheet.GetQueryParam("url");
                        }
                        using (var httpClient = new HttpClient())
                        {
                            using (var fileStream = new FileStream(path, FileMode.Create))
                            {
                                var downloadStream = await httpClient.GetStreamAsync(new Uri(model.DataSheet));

                                downloadStream.CopyTo(fileStream);
                            }
                        }
                    }
                    else if (model.DatasheetFile != null)
                    {
                        using (var fileStream = new FileStream(path, FileMode.Create))
                        {
                            await model.DatasheetFile.CopyToAsync(fileStream);
                        }
                    }
                }

                if (model.ComponentImage != null)
                {
                    string extension;
                    string path;
                    if (model.ComponentImage.StartsWith("http"))
                    {
                        extension = Path.GetExtension(await GetFileNameFromUrl(model.ComponentImage));
                        component.ComponentPinoutImage = componentSaveName + extension;
                        path = Path.Combine(uploads, component.ComponentPinoutImage);
                        using (var httpClient = new HttpClient())
                        {
                            using (var fileStream = new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite))
                            {
                                var downloadStream = await httpClient.GetStreamAsync(new Uri(model.ComponentPinoutImage));

                                downloadStream.CopyTo(fileStream);
                                using (var thumbnailFileStream = new FileStream(Path.Combine(uploads, componentSaveName + "-254x254" + extension), FileMode.Create))
                                {
                                    Image <Rgba32> image = Image.Load <Rgba32>(fileStream);
                                    image.Resize(new ResizeOptions {
                                        Mode = ResizeMode.Max, Size = new Size(254, 254)
                                    }).Save(thumbnailFileStream);
                                    image.Dispose();
                                }
                            }
                        }
                    }
                    else if (model.ComponentImageFile != null)
                    {
                        extension = Path.GetExtension(model.ComponentImage);
                        component.ComponentImage = componentSaveName + extension;
                        path = Path.Combine(uploads, component.ComponentImage);
                        using (var fileStream = new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite))
                        {
                            await model.ComponentImageFile.CopyToAsync(fileStream);

                            using (var thumbnailFileStream = new FileStream(Path.Combine(uploads, componentSaveName + "-254x254" + extension), FileMode.Create))
                            {
                                Image <Rgba32> image = Image.Load <Rgba32>(fileStream);
                                image.Resize(new ResizeOptions {
                                    Mode = ResizeMode.Max, Size = new Size(254, 254)
                                }).Save(thumbnailFileStream);
                                image.Dispose();
                            }
                        }
                    }
                }

                if (model.ComponentPinoutImage != string.Empty)
                {
                    string path;
                    string extension;
                    if (model.ComponentPinoutImage.StartsWith("http"))
                    {
                        if (model.ComponentPinoutImage.Contains("www.google."))
                        {
                            model.ComponentPinoutImage = model.ComponentPinoutImage.GetQueryParam("url");
                        }
                        extension = Path.GetExtension(await GetFileNameFromUrl(model.ComponentPinoutImage));
                        component.ComponentPinoutImage = componentSaveName + extension;
                        path = Path.Combine(uploads, component.ComponentPinoutImage);
                        using (var httpClient = new HttpClient())
                        {
                            using (var fileStream = new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite))
                            {
                                var downloadStream = await httpClient.GetStreamAsync(new Uri(model.ComponentPinoutImage));

                                await downloadStream.CopyToAsync(fileStream);

                                using (var thumbnailFileStream = new FileStream(Path.Combine(uploads, componentSaveName + "-254x254" + extension), FileMode.Create))
                                {
                                    Image <Rgba32> image = Image.Load <Rgba32>(fileStream);
                                    image.Resize(new ResizeOptions {
                                        Mode = ResizeMode.Max, Size = new Size(254, 254)
                                    }).Save(thumbnailFileStream);
                                    image.Dispose();
                                }
                            }
                        }
                    }
                    else if (model.ComponentPinoutImageFile != null)
                    {
                        extension = Path.GetExtension(model.ComponentPinoutImage);
                        component.ComponentPinoutImage = componentSaveName + extension;
                        path = Path.Combine(uploads, component.ComponentPinoutImage);
                        using (var fileStream = new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite))
                        {
                            await model.ComponentPinoutImageFile.CopyToAsync(fileStream);

                            using (var thumbnailFileStream = new FileStream(Path.Combine(uploads, componentSaveName + "-254x254" + extension), FileMode.Create))
                            {
                                Image <Rgba32> image = Image.Load <Rgba32>(fileStream);
                                image.Resize(new ResizeOptions {
                                    Mode = ResizeMode.Max, Size = new Size(254, 254)
                                }).Save(thumbnailFileStream);
                                image.Dispose();
                            }
                        }
                    }
                }



                _dbContext.Add(component);
                await _dbContext.SaveChangesAsync();

                return(RedirectToAction("Index"));
            }
            return(View(model));
        }