コード例 #1
0
        public async Task <IActionResult> EditProjectConfirm(ProjectClientViewModel model)
        {
            string nvm;

            try
            {
                if (!ModelState.IsValid)
                {
                    nvm = NotificationHandler.SerializeMessage <string>(NotificationHandler.Wrong_Values, contentRootPath);
                    return(RedirectToAction("ProjectList", new { notification = nvm }));
                }
                if (model != null)
                {
                    var currentUser = await _userManager.FindByNameAsync(User.Identity.Name);

                    var doesExist = _dbProject.GetAll().Where(x => (x.Title == model.Title || x.LatinTitle.ToLower() == model.LatinTitle.ToLower()) && x.Id != model.Id).ToList();
                    if (doesExist.Count > 0)
                    {
                        nvm = NotificationHandler.SerializeMessage <string>(NotificationHandler.DuplicatedValue, contentRootPath);
                        return(RedirectToAction("ProjectList", new { notification = nvm }));
                    }
                    var modifyingRecord = _dbProject.FindById(model.Id);
                    if (modifyingRecord != null)
                    {
                        modifyingRecord.Title                  = model.Title;
                        modifyingRecord.LatinTitle             = model.LatinTitle;
                        modifyingRecord.ProjectManagerFullName = model.ProjectManagerFullName;
                        modifyingRecord.CategoryId             = model.CategoryId;
                        modifyingRecord.Price                  = model.Price > 0 ? model.Price : 0;
                        modifyingRecord.Collaborators          = model.Collaborators != null ? model.Collaborators : modifyingRecord.Collaborators;
                        modifyingRecord.ClientId               = model.ClientId;
                        modifyingRecord.Technologies           = model.Technologies;
                        modifyingRecord.Starts                 = model.Starts != null?CustomizeCalendar.PersianToGregorian(model.Starts ?? DateTime.Now) : DateTime.Now;

                        modifyingRecord.Ends = model.Ends != null?CustomizeCalendar.PersianToGregorian(model.Ends ?? DateTime.Now) : DateTime.Now.AddMonths(2);

                        modifyingRecord.Description = model.Description;
                        modifyingRecord.Status      = model.Status;

                        modifyingRecord.ModifiedByUsers = modifyingRecord.ModifiedByUsers + $" - ویرایش شده توسط: {currentUser.UserName} ، در مورخه: {CustomizeCalendar.GregorianToPersianDateTime(DateTime.Now)} \n ";

                        _dbProject.Update(modifyingRecord);

                        nvm = NotificationHandler.SerializeMessage <string>(NotificationHandler.Success_Update, contentRootPath);
                        return(RedirectToAction("ProjectList", new { notification = nvm }));
                    }
                }
                nvm = NotificationHandler.SerializeMessage <string>(NotificationHandler.Failed_Update, contentRootPath);
                return(RedirectToAction("ProjectList", new { notification = nvm }));
            }
            catch (Exception ex)
            {
                nvm = NotificationHandler.SerializeMessage <string>(NotificationHandler.Failed_Operation, contentRootPath);
                return(RedirectToAction("ProjectList", new { notification = nvm }));
            }
        }
コード例 #2
0
        }//end AddProject

        public async Task <IActionResult> AddProjectConfirm(ProjectClientViewModel model)
        {
            string nvm;

            try
            {
                if (!ModelState.IsValid)
                {
                    nvm = NotificationHandler.SerializeMessage <string>(NotificationHandler.Wrong_Values, contentRootPath);
                    return(RedirectToAction("Add", new { notification = nvm }));
                }
                if (model != null)
                {
                    var currentUser = await _userManager.FindByNameAsync(User.Identity.Name);

                    if (currentUser != null)
                    {
                        Projects newProject = new Projects()
                        {
                            Title                             = model.Title,
                            LatinTitle                        = model.LatinTitle,
                            Status                            = model.Status,
                            CategoryId                        = model.CategoryId > 0 ? model.CategoryId : null,
                            ClientId                          = model.ClientId > 0 ? model.ClientId : null,
                            Collaborators                     = model.Collaborators,
                            ProjectManagerFullName            = model.ProjectManagerFullName,
                            Description                       = model.Description,
                            DefinedByUser                     = currentUser,
                            Starts                            = model.Starts != null?CustomizeCalendar.PersianToGregorian(model.Starts ?? DateTime.Now) : DateTime.Now,
                                                         Ends = model.Ends != null?CustomizeCalendar.PersianToGregorian(model.Ends ?? DateTime.Now) : DateTime.Now.AddMonths(2),
                                                                         Technologies = model.Technologies,
                                                                         Price        = model.Price > 0 ? model.Price : 0
                        };
                        _dbProject.Insert(newProject);
                        nvm = NotificationHandler.SerializeMessage <string>(NotificationHandler.Success_Insert, contentRootPath);
                        return(RedirectToAction("AddProject", new { notification = nvm }));
                    }
                }
                nvm = NotificationHandler.SerializeMessage <string>(NotificationHandler.Failed_Insert, contentRootPath);
                return(RedirectToAction("AddProject", new { notification = nvm }));
            }
            catch (Exception ex)
            {
                nvm = NotificationHandler.SerializeMessage <string>(NotificationHandler.Failed_Operation, contentRootPath);
                return(RedirectToAction("AddProject", new { notification = nvm }));
            }
        }//end AddProjectConfirm
コード例 #3
0
        public static string DateCode()
        {
            var    p            = CustomizeCalendar.GToPDateTime(DateTime.Now);
            Random rnd          = new Random();
            int    r            = rnd.Next(1, 999);
            string combinedText = $"{p.Ticks.ToString().Substring(0,11)}{r}";

            //string combinedText = $"{p.Year.ToString().Substring(2,2)}{p.Month}{p.DayOfYear}{p.Hour}{p.Minute}{p.Second}";
            if (combinedText.Length < 14)
            {
                for (int i = 0; i < 14 - combinedText.Length; i++)
                {
                    combinedText = combinedText + "0";
                }
            }
            return(combinedText);
        }
コード例 #4
0
        public async Task <IActionResult> EditUserConfirm(RegisterViewModel model)
        {
            string nvm;

            if (!ModelState.IsValid)
            {
                nvm = NotificationHandler.SerializeMessage <string>(NotificationHandler.Wrong_Values, contentRootPath);
                return(RedirectToAction("UserList", new { notification = nvm }));
            }
            var editUser = await _userManager.FindByNameAsync(model.UserName);

            if (editUser == null)
            {
                nvm = NotificationHandler.SerializeMessage <string>(NotificationHandler.Failed_Update, contentRootPath);
                return(RedirectToAction("UserList", new { notification = nvm }));
            }
            var greDate = CustomizeCalendar.PersianToGregorian(model.Dateofbirth ?? DateTime.Now);

            //update user
            editUser.FirstName            = model.Firstname;
            editUser.LastName             = model.Lastname;
            editUser.Gendre               = model.Gender.Value ? 1 : 0;
            editUser.DateOfBirth          = greDate;
            editUser.PhoneNumber          = model.Phonenumber;
            editUser.PhoneNumberConfirmed = model.PhoneConfirm;
            editUser.EmailConfirmed       = model.EmailConfirm;
            editUser.Status               = model.Status;

            var status = await _userManager.UpdateAsync(editUser);

            if (status.Succeeded)
            {
                //remove user from all roles
                var roles = await _userManager.GetRolesAsync(editUser);

                await _userManager.RemoveFromRolesAsync(editUser, roles.ToArray());

                //add to role
                await _userManager.AddToRoleAsync(editUser, model.RoleName);

                nvm = NotificationHandler.SerializeMessage <string>(NotificationHandler.Success_Update, contentRootPath);
                return(RedirectToAction("UserList", new { notification = nvm }));
            }
            nvm = NotificationHandler.SerializeMessage <string>(NotificationHandler.Failed_Update, contentRootPath);
            return(RedirectToAction("UserList", new { notification = nvm }));
        }
コード例 #5
0
        public async Task <IActionResult> SignupConfirm(RegisterViewModel model)
        {
            string nvm;

            if (!ModelState.IsValid)
            {
                nvm = NotificationHandler.SerializeMessage <string>(NotificationHandler.Wrong_Values, contentRootPath);
                return(RedirectToAction("Signup", new { notification = nvm }));
            }
            var CurrentUser = await _userManager.FindByNameAsync(model.UserName);

            if (CurrentUser != null)
            {
                nvm = NotificationHandler.SerializeMessage <string>(NotificationHandler.DuplicatedValue, contentRootPath);
                return(RedirectToAction("Signup", new { notification = nvm }));
            }
            var             greDate = CustomizeCalendar.PersianToGregorian(model.Dateofbirth ?? DateTime.Now);
            ApplicationUser NewUser = new ApplicationUser()
            {
                UserName             = model.UserName,
                Email                = model.UserName,
                FirstName            = model.Firstname,
                LastName             = model.Lastname,
                Gendre               = model.Gender.Value ? 1 : 0,
                DateOfBirth          = greDate,
                PhoneNumber          = model.Phonenumber,
                PhoneNumberConfirmed = model.PhoneConfirm,
                EmailConfirmed       = model.EmailConfirm,
                Status               = model.Status,
            };
            var status = await _userManager.CreateAsync(NewUser, model.Password);

            if (status.Succeeded)
            {
                await _userManager.AddToRoleAsync(NewUser, model.RoleName);

                nvm = NotificationHandler.SerializeMessage <string>(NotificationHandler.Success_Insert, contentRootPath);
                return(RedirectToAction("UserList", new { notification = nvm }));
            }
            nvm = NotificationHandler.SerializeMessage <string>(NotificationHandler.Failed_Operation, contentRootPath);
            return(RedirectToAction("Signup", new { notification = nvm }));
        }
コード例 #6
0
        }//end EditUser

        public async Task <IActionResult> EditUserConfirm(SignupViewModel model, List <IFormFile> img, string Id)
        {
            string nvm;
            var    user = await _userManager.FindByIdAsync(Id);

            var userModifier = await _userManager.FindByNameAsync(User.Identity.Name);

            try
            {
                if (user == null)
                {
                    nvm = NotificationHandler.SerializeMessage <string>(NotificationHandler.Record_Not_Exist, contentRootPath);
                    return(RedirectToAction("UserList", new { notification = nvm }));
                }


                //check if other users want to change 'Admin' deny their access to do that
                var userModifierRoleId   = _db.UserRoles.Where(x => x.UserId == userModifier.Id).FirstOrDefault().RoleId;
                var userModifierRoleName = _db.Roles.Where(x => x.Id == userModifierRoleId).FirstOrDefault().Name;

                var userRoleId   = _db.UserRoles.Where(x => x.UserId == Id).FirstOrDefault().RoleId;
                var userRoleName = _db.Roles.Where(x => x.Id == userRoleId).FirstOrDefault().Name;
                if (userModifierRoleName != "Admin" && userRoleName == "Admin")
                {
                    nvm = NotificationHandler.SerializeMessage <string>(NotificationHandler.Access_denied, contentRootPath);
                    return(RedirectToAction("UserList", new { notification = nvm }));
                }

                if (model.CurrentPassword != null && model.NewPassword != null && model.ConfirmNewPassword != null)
                {
                    if (userModifier == user || userModifierRoleName == "Admin")
                    {
                        var statusPasswordChange = await _userManager.ChangePasswordAsync(user, model.CurrentPassword, model.NewPassword);
                    }
                }

                //Get Backup From The User Into 'UserModified' Table
                var userJSONForBackup = JsonConvert.SerializeObject(user);

                using (var transaction = _db.Database.BeginTransaction())
                {
                    UserModified UM = new UserModified()
                    {
                        LastUserBackupJson = userJSONForBackup,
                        ModifedByUserId    = userModifier.Id,
                        UserId             = user.Id,
                        Comment            = "Edited by " + userModifier.UserName
                    };
                    _dbUserModified.Insert(UM);

                    var greDate = CustomizeCalendar.PersianToGregorian(model.Dateofbirth ?? DateTime.Now);
                    user.FirstName   = model.Firstname;
                    user.LastName    = model.Lastname;
                    user.Email       = model.Email;
                    user.PhoneNumber = model.Phonenumber;
                    user.SpecialUser = model.Specialuser;
                    //user.Status = model.Status;
                    user.DateOfBirth = greDate;
                    if (model.Gender == true)
                    {
                        user.Gendre = 1; //male
                    }
                    else if (model.Gender == false)
                    {
                        user.Gendre = 2; //female
                    }
                    else
                    {
                        user.Gendre = null; //not specified
                    }

                    if (userRoleName != model.RoleName)
                    {
                        if (userModifierRoleName != "Admin")
                        {
                            nvm = NotificationHandler.SerializeMessage <string>(NotificationHandler.Access_denied, contentRootPath);
                            return(RedirectToAction("UserList", new { notification = nvm }));
                        }
                    }
                    //only admin can disable other users
                    if (userModifierRoleName == "Admin")
                    {
                        //"*****@*****.**" will never be disable
                        if (user.UserName == MainAdmin)
                        {
                            user.Status = model.Status;
                            //change user role
                            IdentityResult rol_status_1 = new IdentityResult(), rol_status_2 = new IdentityResult();
                            if (userRoleName != model.RoleName)
                            {
                                rol_status_1 = await _userManager.RemoveFromRoleAsync(user, userRoleName);

                                rol_status_2 = await _userManager.AddToRoleAsync(user, model.RoleName);
                            }
                        }
                        else
                        {
                            nvm = NotificationHandler.SerializeMessage <string>(NotificationHandler.Access_denied, contentRootPath);
                            return(RedirectToAction("UserList", new { notification = nvm }));
                        }
                    }


                    //--------------------------------
                    if (img.Count > 0)
                    {
                        //disable the last images of the user
                        var doesUserHaveImage = _db.UserImage.Where(x => x.UserId == Id).ToList();
                        if (doesUserHaveImage.Count > 0)
                        {
                            foreach (var item in doesUserHaveImage)
                            {
                                var lastImagesOfUser = _dbUserImage.FindById(item.Id);
                                lastImagesOfUser.Status  = false;
                                lastImagesOfUser.Comment = $"Edited by {userModifier.UserName}";
                                _db.UserImage.Update(lastImagesOfUser);
                            }
                        }
                        //--------------------------------
                        //add new image of the user
                        UserImage userImage = new UserImage()
                        {
                            UserId  = Id,
                            Caption = model.Lastname + "_" + DateTime.Now
                        };
                        img.ForEach(x =>
                        {
                            if (x != null)
                            {
                                byte[] b = new byte[x.Length];
                                x.OpenReadStream().Read(b, 0, b.Length);
                                userImage.Image = b;

                                //Make Thumbnail
                                MemoryStream mem1 = new MemoryStream(b);
                                Image img2        = Image.FromStream(mem1);
                                Bitmap bmp        = new Bitmap(img2, 120, 120);
                                MemoryStream mem2 = new MemoryStream();
                                bmp.Save(mem2, System.Drawing.Imaging.ImageFormat.Jpeg);
                                userImage.ImageThumbnail = mem2.ToArray();
                            }
                        });
                        _dbUserImage.Insert(userImage);
                    }



                    //--------------------------------
                    var status = await _userManager.UpdateAsync(user);

                    if (status.Succeeded)
                    {
                        transaction.Commit(); //Save the new records in 'User' table and 'UserImage' table

                        nvm = NotificationHandler.SerializeMessage <string>(NotificationHandler.Success_Update, contentRootPath);
                        return(RedirectToAction("UserList", new { notification = nvm }));
                    }
                }
                nvm = NotificationHandler.SerializeMessage <string>(NotificationHandler.Failed_Update, contentRootPath);
                return(RedirectToAction("UserList", new { notification = nvm }));
            }
            catch (Exception)
            {
                nvm = NotificationHandler.SerializeMessage <string>(NotificationHandler.Failed_Update, contentRootPath);
                return(RedirectToAction("UserList", new { notification = nvm }));
            }
        }//end EditUserConfirm
コード例 #7
0
        }//end Signup

        public async Task <IActionResult> SignupConfirm(SignupViewModel model, List <IFormFile> img)
        {
            string nvm;

            try
            {
                if (!User.Identity.IsAuthenticated)
                {
                    nvm = NotificationHandler.SerializeMessage <string>(NotificationHandler.Require_Login, contentRootPath);
                    return(RedirectToAction("Signin", new { notification = nvm }));
                }
                if (!ModelState.IsValid)
                {
                    nvm = NotificationHandler.SerializeMessage <string>(NotificationHandler.Wrong_Values, contentRootPath);
                    return(RedirectToAction("Signup", new { notification = nvm }));
                }
                else
                {
                    if (model != null)
                    {
                        var chkUser = await _userManager.FindByNameAsync(model.Username);

                        ApplicationUser chkEmail;
                        if (model.Email != null)
                        {
                            chkEmail = await _userManager.FindByEmailAsync(model.Email);
                        }
                        else
                        {
                            chkEmail = null;
                        }
                        if (chkUser == null && chkEmail == null)
                        {
                            var currentUser = await _userManager.FindByNameAsync(User.Identity.Name);

                            var greDate = CustomizeCalendar.PersianToGregorian(model.Dateofbirth ?? DateTime.Now);

                            //create new user
                            ApplicationUser newUser = new ApplicationUser()
                            {
                                FirstName     = model.Firstname,
                                LastName      = model.Lastname,
                                UserName      = model.Username,
                                Email         = model.Email,
                                PhoneNumber   = model.Phonenumber,
                                SpecialUser   = model.Specialuser,
                                Status        = model.Status,
                                DefinedByUser = currentUser,
                                DateOfBirth   = greDate,
                                Rank          = 1,
                                NationalCode  = model.Nationalcode
                            };
                            if (model.Gender == true)
                            {
                                newUser.Gendre = 1; //male
                            }
                            else if (model.Gender == false)
                            {
                                newUser.Gendre = 2; //female
                            }
                            else
                            {
                                newUser.Gendre = null; //not specified
                            }

                            var user_status = await _userManager.CreateAsync(newUser, model.Password);

                            if (user_status.Succeeded)
                            {
                                await _userManager.AddToRoleAsync(newUser, "SuperVisor");

                                //Add image of user in 'UserImage' table, seperately
                                if (img.Count > 0)
                                {
                                    var newAddedUser = await _userManager.FindByNameAsync(newUser.UserName);

                                    if (newAddedUser != null)
                                    {
                                        try
                                        {
                                            UserImage userImage = new UserImage()
                                            {
                                                UserId  = newAddedUser.Id,
                                                Caption = newAddedUser.LastName + "_" + DateTime.Now
                                            };
                                            img.ForEach(x =>
                                            {
                                                if (x != null)
                                                {
                                                    byte[] b = new byte[x.Length];
                                                    x.OpenReadStream().Read(b, 0, b.Length);
                                                    userImage.Image = b;

                                                    //Make Thumbnail
                                                    MemoryStream mem1 = new MemoryStream(b);
                                                    Image img2        = Image.FromStream(mem1);
                                                    Bitmap bmp        = new Bitmap(img2, 120, 120);
                                                    MemoryStream mem2 = new MemoryStream();
                                                    bmp.Save(mem2, System.Drawing.Imaging.ImageFormat.Jpeg);
                                                    userImage.ImageThumbnail = mem2.ToArray();
                                                }
                                            });
                                            _db.UserImage.Add(userImage);
                                            _db.SaveChanges();
                                        }
                                        catch (Exception)
                                        {
                                            nvm = NotificationHandler.SerializeMessage <string>(NotificationHandler.Failed_Insert_Image, contentRootPath);
                                            return(RedirectToAction("Signup", new { notification = nvm }));
                                        }
                                    }
                                }

                                nvm = NotificationHandler.SerializeMessage <string>(NotificationHandler.Success_Insert, contentRootPath);
                                return(RedirectToAction("Signup", new { notification = nvm }));
                            }
                            nvm = NotificationHandler.SerializeMessage <string>(NotificationHandler.Failed_Insert, contentRootPath);
                            return(RedirectToAction("Signup", new { notification = nvm }));
                        }
                        else
                        {
                            nvm = NotificationHandler.SerializeMessage <string>(NotificationHandler.Failed_Insert, contentRootPath);
                            return(RedirectToAction("Signup", new { notification = nvm }));
                        }
                    }
                    else
                    {
                        nvm = NotificationHandler.SerializeMessage <string>(NotificationHandler.Failed_Operation, contentRootPath);
                        return(RedirectToAction("Signup", new { notification = nvm }));
                    }
                }
            }
            catch (Exception)
            {
                nvm = NotificationHandler.SerializeMessage <string>(NotificationHandler.Failed_Operation, contentRootPath);
                return(RedirectToAction("Signup", new { notification = nvm }));
            }
        }//end SignupConfirm
        public async Task <IActionResult> EditSliderConfirm(TopSliderViewModel model)
        {
            string nvm;

            try
            {
                if (model != null)
                {
                    var currentUser = await _userManager.FindByNameAsync(User.Identity.Name);

                    var _topSlider = dbTopSlider.FindById(model.Id);
                    if (_topSlider != null)
                    {
                        _topSlider.Title              = model.Title;
                        _topSlider.Summery            = model.Summery;
                        _topSlider.Description        = model.Description;
                        _topSlider.AltName            = model.AltName;
                        _topSlider.Link               = model.Link;
                        _topSlider.ScreenResulationId = model.ScreenResulationId > 0 ? model.ScreenResulationId : null;
                        _topSlider.Status             = model.Status;
                        _topSlider.Active             = model.Active;
                        _topSlider.Priotity           = model.Priotity;
                        _topSlider.SetForFuture       = model.SetForFuture;
                        _topSlider.HasButton          = model.HasButton;
                        _topSlider.UserId             = currentUser.Id;

                        if (model.ConnectedCategoryId > 0)
                        {
                            _topSlider.ConnectedCategoryId = model.ConnectedCategoryId;
                        }
                        if (model.ConnectedBrandId > 0)
                        {
                            _topSlider.ConnectedBrandId = model.ConnectedBrandId;
                        }
                        if (model.ConnectedProductId > 0)
                        {
                            _topSlider.ConnectedProductId = model.ConnectedProductId;
                        }
                        if (model.ShowDateTime.ToString().Length > 1 && model.SetForFuture == true)
                        {
                            _topSlider.ShowDateTime = CustomizeCalendar.PersianToGregorian(model.ShowDateTime ?? DateTime.Now);
                        }
                        if (model.ExpireDateTime.ToString().Length > 1 && model.SetForFuture == true)
                        {
                            _topSlider.ExpireDateTime = CustomizeCalendar.PersianToGregorian(model.ExpireDateTime ?? DateTime.Now);
                        }
                        if (model.HasButton && model.ButtonContent != null)
                        {
                            _topSlider.ButtonContent = model.ButtonContent;
                            _topSlider.ButtonLink    = model.ButtonLink;
                        }



                        string folderPath = _configuration.GetSection("DefaultPaths").GetSection("SliderImage").Value;
                        string uploads    = Path.Combine(contentRootPath, folderPath);
                        bool   exists     = System.IO.Directory.Exists(uploads);
                        if (!exists)
                        {
                            System.IO.Directory.CreateDirectory(uploads);
                        }
                        var files = HttpContext.Request.Form.Files;
                        if (files.Count > 0)
                        {
                            FileManager.DeleteFile(contentRootPath, _topSlider.ImagePath);
                        }
                        foreach (var Image in files)
                        {
                            if (Image != null && Image.Length > 0)
                            {
                                var file = Image;

                                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);

                                        _topSlider.ImagePath = folderPath + fileName;
                                    }
                                }
                            }
                        }

                        dbTopSlider.Update(_topSlider); //edit record successfully

                        nvm = NotificationHandler.SerializeMessage <string>(NotificationHandler.Success_Update, contentRootPath);
                        return(RedirectToAction("ShowSliders", new { notification = nvm }));
                    }
                }
                nvm = NotificationHandler.SerializeMessage <string>(NotificationHandler.Failed_Update, contentRootPath);
                return(RedirectToAction("ShowSliders", new { notification = nvm }));
            }
            catch (Exception ex)
            {
                nvm = NotificationHandler.SerializeMessage <string>(NotificationHandler.Failed_Operation, contentRootPath);
                return(RedirectToAction("ShowSliders", new { notification = nvm }));
            }
        }
        public async Task <IActionResult> InsertSliderImageConfirm(TopSliderViewModel model)
        {
            string nvm;

            try
            {
                if (!ModelState.IsValid)
                {
                    nvm = NotificationHandler.SerializeMessage <string>(NotificationHandler.Wrong_Values, contentRootPath);
                    return(RedirectToAction("InsertSliderImage", new { notification = nvm }));
                }
                if (model != null)
                {
                    var currentUser = await _userManager.FindByNameAsync(User.Identity.Name);

                    TopSlider TS = new TopSlider()
                    {
                        Title              = model.Title,
                        Summery            = model.Summery,
                        Description        = model.Description,
                        AltName            = model.AltName,
                        Link               = model.Link,
                        ScreenResulationId = model.ScreenResulationId > 0 ? model.ScreenResulationId : null,
                        Status             = model.Status,
                        Active             = model.Active,
                        Priotity           = model.Priotity,
                        SetForFuture       = model.SetForFuture,
                        HasButton          = model.HasButton,
                        UserId             = currentUser.Id
                    };
                    if (model.ConnectedCategoryId > 0)
                    {
                        TS.ConnectedCategoryId = model.ConnectedCategoryId;
                    }
                    if (model.ConnectedBrandId > 0)
                    {
                        TS.ConnectedBrandId = model.ConnectedBrandId;
                    }
                    if (model.ConnectedProductId > 0)
                    {
                        TS.ConnectedProductId = model.ConnectedProductId;
                    }
                    if (model.ShowDateTime.ToString().Length > 1 && model.SetForFuture == true)
                    {
                        TS.ShowDateTime = CustomizeCalendar.PersianToGregorian(model.ShowDateTime ?? DateTime.Now);
                    }
                    if (model.ExpireDateTime.ToString().Length > 1 && model.SetForFuture == true)
                    {
                        TS.ExpireDateTime = CustomizeCalendar.PersianToGregorian(model.ExpireDateTime ?? DateTime.Now);
                    }
                    if (model.HasButton && model.ButtonContent != null)
                    {
                        TS.ButtonContent = model.ButtonContent;
                        TS.ButtonLink    = model.ButtonLink;
                    }

                    string folderPath = _configuration.GetSection("DefaultPaths").GetSection("SliderImage").Value;
                    string uploads    = Path.Combine(contentRootPath, folderPath);
                    bool   exists     = System.IO.Directory.Exists(uploads);
                    if (!exists)
                    {
                        System.IO.Directory.CreateDirectory(uploads);
                    }

                    var files = HttpContext.Request.Form.Files;
                    foreach (var Image in files)
                    {
                        if (Image != null && Image.Length > 0)
                        {
                            var file = Image;

                            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);

                                    TS.ImagePath = folderPath + fileName;
                                }
                            }
                        }
                    }

                    dbTopSlider.Insert(TS); //save record successfully

                    nvm = NotificationHandler.SerializeMessage <string>(NotificationHandler.Success_Insert, contentRootPath);
                    return(RedirectToAction("InsertSliderImage", new { notification = nvm }));
                }
                nvm = NotificationHandler.SerializeMessage <string>(NotificationHandler.Failed_Insert, contentRootPath);
                return(RedirectToAction("InsertSliderImage", new { notification = nvm }));
            }
            catch (Exception)
            {
                nvm = NotificationHandler.SerializeMessage <string>(NotificationHandler.Failed_Operation, contentRootPath);
                return(RedirectToAction("InsertSliderImage", new { notification = nvm }));
            }
        }//end InsertSliderImageConfirm
コード例 #10
0
        }//end EditCategory

        public async Task <IActionResult> EditCategoryConfirm(CategoryViewModel model)
        {
            string nvm;

            try
            {
                if (!ModelState.IsValid)
                {
                    nvm = NotificationHandler.SerializeMessage <string>(NotificationHandler.Wrong_Values, contentRootPath);
                    return(RedirectToAction("CategoryList", new { notification = nvm }));
                }
                if (model != null)
                {
                    var currentUser = await _userManager.FindByNameAsync(User.Identity.Name);

                    var doesExist = _dbCategory.GetAll().Where(x => (x.Name == model.Name || x.LatinName.ToLower() == model.LatinName.ToLower()) && x.Id != model.Id).ToList();
                    if (doesExist.Count > 0)
                    {
                        nvm = NotificationHandler.SerializeMessage <string>(NotificationHandler.DuplicatedValue, contentRootPath);
                        return(RedirectToAction("CategoryList", new { notification = nvm }));
                    }
                    var modifyingCategory = _dbCategory.FindById(model.Id);
                    if (modifyingCategory != null)
                    {
                        modifyingCategory.Name          = model.Name;
                        modifyingCategory.LatinName     = model.LatinName;
                        modifyingCategory.Status        = model.Status;
                        modifyingCategory.ModifiedBy    = modifyingCategory.ModifiedBy + $" - ویرایش شده توسط: {currentUser.UserName} ، در مورخه: {CustomizeCalendar.GregorianToPersianDateTime(DateTime.Now)} \n ";
                        modifyingCategory.ParentId      = model.ParentId;
                        modifyingCategory.AliasName     = model.AliasName;
                        modifyingCategory.TitleAltName  = model.TitleAltName;
                        modifyingCategory.Description   = model.Description;
                        modifyingCategory.ConnectedLink = model.ConnectedLink;

                        _dbCategory.Update(modifyingCategory);

                        nvm = NotificationHandler.SerializeMessage <string>(NotificationHandler.Success_Update, contentRootPath);
                        return(RedirectToAction("CategoryList", new { notification = nvm }));
                    }
                }
                nvm = NotificationHandler.SerializeMessage <string>(NotificationHandler.Failed_Update, contentRootPath);
                return(RedirectToAction("CategoryList", new { notification = nvm }));
            }
            catch (Exception ex)
            {
                nvm = NotificationHandler.SerializeMessage <string>(NotificationHandler.Failed_Operation, contentRootPath);
                return(RedirectToAction("CategoryList", new { notification = nvm }));
            }
        }//end EditCategoryConfirm