Esempio n. 1
0
        public ActionResult Index(EditUserViewModel vm)
        {
            if (!ModelState.IsValid)
            {
                return(View(vm));
            }

            User user = _context.Users.ToList().First();

            user.Email    = vm.Email;
            user.Password = PasswordHasher.GetHash(vm.NewPassword);

            _context.SaveChanges();

            return(RedirectToAction("Index", "Settings"));
        }
Esempio n. 2
0
        public ActionResult Create(CreateAuthorViewModel vm)
        {
            if (!ModelState.IsValid)
            {
                return(View(vm));
            }

            string [] allowedExtensions = new string[] { ".jpg", ".png", ".jpeg" };

            string pic = Guid.NewGuid().ToString() + Path.GetFileName(vm.Image.FileName);

            bool find = false;

            foreach (string extension in allowedExtensions)
            {
                if (pic.Contains(extension))
                {
                    find = true;
                    break;
                }
            }

            if (!find)
            {
                ModelState.AddModelError("Errors", "არასწორი ფორმატი");
                return(View(vm));
            }

            string path = Path.Combine(Server.MapPath("~/Content/uploads/"), pic);

            vm.Image.SaveAs(path);


            _context.Authors.Add(new Author
            {
                FirstName    = vm.FirstName,
                LastName     = vm.LastName,
                Email        = vm.Email,
                Image        = pic,
                BehanceLink  = vm.BehanceLink,
                GithubLink   = vm.GithubLink,
                LinkedinLink = vm.LinkedinLink,
                DribbleLink  = vm.DribbleLink
            });

            _context.SaveChanges();

            return(RedirectToAction("Index", "Author"));
        }
Esempio n. 3
0
        public ActionResult Details(EditTagViewModel vm)
        {
            if (!ModelState.IsValid)
            {
                return(View(vm));
            }

            Tag tag = _context.Tags.Where(x => x.Id == vm.Id).FirstOrDefault();

            if (tag == null)
            {
                return(RedirectToAction("Index", "Error"));
            }

            tag.Name = vm.Name;
            _context.SaveChanges();

            return(RedirectToAction("Index", "Tags"));
        }
        public ActionResult Create(CreateProjectViewModel vm)
        {
            if (!ModelState.IsValid)
            {
                vm.Authors = _context.Authors.ToList();
                vm.Tags    = _context.Tags.ToList();
                return(View(vm));
            }

            //validate files

            foreach (var file in vm.Photos)
            {
                if (!ValidateFile(file))
                {
                    ModelState.AddModelError("error", "არასწორია ფაილის ფორმატი");
                    vm.Authors = _context.Authors.ToList();
                    vm.Tags    = _context.Tags.ToList();
                    return(View(vm));
                }
            }


            //transaction
            using (var dbContextTransaction = _context.Database.BeginTransaction())
            {
                try
                {
                    // add project
                    Project project = new Project
                    {
                        AuthorId    = vm.AuthorId,
                        Description = vm.Description,
                        Name        = vm.Name,
                        Published   = DateTime.Now
                    };

                    _context.Projects.Add(project);
                    _context.SaveChanges();

                    // add tags
                    foreach (int tagId in vm.TagIds)
                    {
                        ProjectTag tag = new ProjectTag
                        {
                            ProjectId = project.Id,
                            TagId     = tagId
                        };
                        _context.ProjectTags.Add(tag);
                    }
                    _context.SaveChanges();

                    //upload files
                    for (int i = 0; i < vm.Photos.Length; i++)
                    {
                        ProjectImage projectImage = new ProjectImage
                        {
                            ProjectId = project.Id,
                            IsMain    = (vm.Primary == i) ? 1 : 0,
                            ImageUrl  = UploadFile(vm.Photos[i])
                        };
                        _context.ProjectImages.Add(projectImage);
                    }

                    _context.SaveChanges();

                    dbContextTransaction.Commit();
                }
                catch (Exception)
                {
                    dbContextTransaction.Rollback();
                    throw;
                }
            }

            return(RedirectToAction("Index", "Project"));
        }