private void AddNewDepartment()
 {
     new List <Department>()
     {
         new Department()
         {
             Name = "IT"
         },
         new Department()
         {
             Name = "Job"
         },
         new Department()
         {
             Name = "Finance"
         }
     }.ForEach(x =>
     {
         if (_context.Departments.Any(s => s.Name == x.Name) == false)
         {
             _context.Add(x);
         }
         else
         {
             Console.WriteLine(x.Name + " already exists!");
         }
     });
     _context.SaveChanges();
 }
Esempio n. 2
0
        public ActionResult PutDepartment(int id, /*[FromBody]*/ Department Department)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            Department dept = _context.Departments.SingleOrDefault(c => c.Id == id);

            if (dept == null)
            {
                return(NotFound());
            }

            dept.Name = Department.Name;
            _context.SaveChanges();
            return(Ok("Updated successfully!"));
        }
        public ActionResult Create(NewDocumentViewModel viewModel, List <HttpPostedFileBase> docs, List <HttpFileCollectionBase> thumbnails)
        {
            if (!ModelState.IsValid)
            {
                GetFormatList();
                GetCategoryList();

                return(View("Create", viewModel));
            }

            var FormatId   = _context.Formats.Single(t => t.Id == viewModel.FormatID).Id;
            var CategoryId = _context.Categories.Single(c => c.Id == viewModel.CategoryID).Id;


            //foreach (var file in docs)
            //{
            var file      = docs[0];
            var thumbnail = docs[1];

            if (file != null && file.ContentLength > 0)
            {
                try
                {
                    string path = Path.Combine(Server.MapPath("~/Uploads"),
                                               Path.GetFileName(file.FileName));
                    file.SaveAs(path);

                    string thumb = Path.Combine(Server.MapPath("~/Thumbnails"),
                                                Path.GetFileName(thumbnail.FileName));

                    thumbnail.SaveAs(thumb);

                    var upload = new Upload
                    {
                        ImageName     = file.FileName,
                        ImagePath     = path,
                        ThumbnailPath = thumb
                    };
                    _context.Uploads.Add(upload);

                    var document = new Documents
                    {
                        Name       = viewModel.Name,
                        Author     = viewModel.Author,
                        Excerpt    = viewModel.Excerpt,
                        FormatID   = FormatId,
                        CategoryID = CategoryId,
                        UploadID   = upload.UploadID
                    };

                    _context.Documents.Add(document);
                    _context.SaveChanges();
                }
                catch (Exception ex)
                {
                    ViewBag.Message = "ERROR:" + ex.Message.ToString();
                }
            }

            else
            {
                ViewBag.Message = "You have not specified a file.";
            }

            //}

            return(RedirectToAction("AfterLogin", "Home"));
        }