public async Task <IActionResult> Info()
        {
            AccountSettingModel model = new AccountSettingModel();
            var accountExists         = await _commonContext.CommonAccounts.Where(x => x.Id == CommonAccount.Id && x.OwnerUserId == User.GetLoggedInUserId().Value).SingleOrDefaultAsync();

            model = Mapper.Map <AccountSettingModel>(accountExists);
            if (model.AttachmentId != null)
            {
                var attach = await _accountCtx.Attachments.Include(x => x.Citations).SingleOrDefaultAsync(x => x.Id == model.AttachmentId);

                if (attach != null)
                {
                    //Read the file from AWS Bucket
                    var cityAppFile = await _fileService.ReadFile(attach.Key, _appSettings.AWSAccessKeyID, _appSettings.AWSSecretKey, _appSettings.AmazonS3Bucket);

                    if (cityAppFile.FileBytes != null)
                    {
                        cityAppFile.FileStream.Position = 0;

                        // Convert byte[] to Base64 String
                        model.ImageName = Convert.ToBase64String(cityAppFile.FileBytes);
                    }
                }
            }

            return(View(model));
        }
Beispiel #2
0
        public IActionResult Settings(AccountSettingModel setting)
        {
            var token = Request.Cookies["token"];

            User LoggedUser = _auth.User;

            if (LoggedUser.Token != token)
            {
                return(BadRequest());
            }

            string FileName;

            if (ModelState.IsValid)
            {
                LoggedUser.Name        = setting.Name;
                LoggedUser.PhoneNumber = setting.PhoneNumber;


                if (LoggedUser.UserTypeID > 1) // if user type is ev sahibi or makler
                {
                    LoggedUser.Surname = setting.Surname;
                }

                if (LoggedUser.UserTypeID == 1) //if usertype is Agentlik
                {
                    LoggedUser.Adress       = setting.Adress;
                    LoggedUser.AboutCompany = setting.AboutCompany;
                }

                FileStream fs = null;

                if (setting.Photo != null)
                {
                    string UploadsFolder = Path.Combine(_hosting.WebRootPath, "img", "users");
                    FileName = Guid.NewGuid() + "_" + setting.Photo.FileName;
                    string FilePath = Path.Combine(UploadsFolder, FileName);
                    fs = new FileStream(FilePath, FileMode.OpenOrCreate);
                    setting.Photo.CopyTo(fs);
                    LoggedUser.Logo = FileName;

                    fs.Close();
                }



                _context.Entry(LoggedUser).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
                _context.SaveChanges();
                TempData["Success"] = "Məlumatlar müvəffəqiyyətlə daxil etdiniz...";
                return(RedirectToAction("index", "home"));
            }

            AccountIndexViewModel data = new AccountIndexViewModel
            {
                Setting   = setting,
                Breadcumb = new BreadcumbViewModel
                {
                    Title = "Tənzimləmələr",
                    Path  = new List <BreadcumbItemViewModel>()
                }
            };
            BreadcumbItemViewModel home = new BreadcumbItemViewModel
            {
                Name       = "Ana səhifə",
                Controller = "Home",
                Action     = "index"
            };
            BreadcumbItemViewModel settings = new BreadcumbItemViewModel
            {
                Name = "Tənzimləmələr"
            };

            data.Breadcumb.Path.Add(home);
            data.Breadcumb.Path.Add(settings);

            ViewBag.Partial = data.Breadcumb;

            return(View("~/Views/Account/Profile.cshtml"));
        }
        public async Task <IActionResult> Info(AccountSettingModel model)
        {
            ModelState.Remove("AttachmentId");
            if (ModelState.IsValid)
            {
                var accountExists = await _commonContext.CommonAccounts.AsNoTracking().Where(x => x.Name.ToLower() == model.Name.ToLower() && x.OwnerUserId != model.Id).AnyAsync();

                if (accountExists)
                {
                    ModelState.AddModelError(nameof(model.Name), "Account name already exists");
                    return(View(model));
                }

                //Get current file extension
                if (model.files != null)
                {
                    if (model.AttachmentId != null)
                    {
                        var attach = await _accountCtx.Attachments.Include(x => x.Citations).SingleOrDefaultAsync(x => x.Id == model.AttachmentId);

                        if (attach != null)
                        {
                            //Delete the file from AWS Bucket and Database
                            var cityAppFile = await _fileService.DeleteFile(attach.Key, _appSettings.AWSAccessKeyID, _appSettings.AWSSecretKey, _appSettings.AmazonS3Bucket);

                            _accountCtx.Attachments.Remove(attach);
                            _accountCtx.Entry(attach).State = EntityState.Deleted;
                            await _accountCtx.SaveChangesAsync();

                            model.AttachmentId = null;
                        }
                    }


                    List <string> Types = _appSettings.ImageTypes.Split(',').ToList();
                    string        ext   = System.IO.Path.GetExtension(model.files.FileName).ToLower();
                    if (Types.Contains(ext))
                    {
                        if (model.files.Length > _appSettings.ImageSize)
                        {
                            //Convert bytes into MB
                            var size = (_appSettings.ImageSize / 1024f) / 1024f;
                            ModelState.AddModelError(string.Empty, $"File size  not be greater than {size} MB.");
                            return(View(model));
                        }


                        Guid fileGuid = Guid.NewGuid();
                        using (var fileStream = model.files.OpenReadStream())
                            using (var ms = new MemoryStream())
                            {
                                fileStream.CopyTo(ms);
                                //convert image into Bytes
                                var fileByte1s = ms.ToArray();

                                var fileNameInFolder = $"accounts/{CommonAccount.Number}/profile/{fileGuid + ext}";

                                //Upload the file on Amazon S3 Bucket
                                var result = await _fileService.UploadFile(fileByte1s, fileNameInFolder, _appSettings.AWSAccessKeyID, _appSettings.AWSSecretKey, true);

                                // put dude in the database
                                Attachment attach = new Attachment()
                                {
                                    AccountId      = CommonAccount.Id,
                                    ContentLength  = model.files.Length,
                                    MimeType       = model.files.ContentType,
                                    FileName       = model.files.FileName,
                                    Key            = fileNameInFolder,
                                    AttachmentType = model.files.FileName.GetAttachmentType(),
                                    CreateUserId   = User.GetLoggedInUserId().Value,
                                    UpdateUserId   = User.GetLoggedInUserId().Value
                                };
                                _accountCtx.Attachments.Add(attach);
                                await _accountCtx.SaveChangesAsync();

                                model.AttachmentId = attach.Id;
                            }
                    }
                    else
                    {
                        ModelState.AddModelError(string.Empty, $"Invalid File Formate {model.files.FileName}.");
                        return(View(model));
                    }
                }

                var commonAccount = await _commonContext.CommonAccounts.Include(x => x.Partition).Where(x => x.OwnerUserId == model.Id && x.Name.ToLower() == model.Name.ToLower()).SingleOrDefaultAsync();

                if (commonAccount != null)
                {
                    commonAccount.Name                    = model.Name;
                    commonAccount.AttachmentId            = model.AttachmentId;
                    commonAccount.ContactNumber           = model.ContactNumber;
                    commonAccount.CityName                = model.City;
                    commonAccount.State                   = model.State;
                    commonAccount.Zip                     = model.Zip;
                    commonAccount.Address1                = model.Address1;
                    commonAccount.Address2                = model.Address2;
                    commonAccount.ContactEmail            = model.ContactEmail;
                    commonAccount.AllowPublicRegistration = model.AllowPublicRegistration;
                    commonAccount.UpdateUserId            = User.GetLoggedInUserId().Value;
                    commonAccount.UpdateUtc               = DateTime.Now;

                    var result = await _commonAccountSvc.UpdateSettingAccount(commonAccount, User.GetLoggedInUserId().Value);

                    //Purge common accounts cache
                    await _cache.RemoveAsync(WebCacheKey.CommonAccounts);

                    await PurgeLoggedInUser();


                    if (model.AttachmentId != null)
                    {
                        var attach = await _accountCtx.Attachments.Include(x => x.Citations).SingleOrDefaultAsync(x => x.Id == model.AttachmentId);

                        if (attach != null)
                        {
                            //Read the file from AWS Bucket
                            var cityAppFile = await _fileService.ReadFile(attach.Key, _appSettings.AWSAccessKeyID, _appSettings.AWSSecretKey, _appSettings.AmazonS3Bucket);

                            if (cityAppFile.FileBytes != null)
                            {
                                // Convert byte[] to Base64 String
                                model.ImageName = Convert.ToBase64String(cityAppFile.FileBytes);
                            }
                        }
                    }
                }
            }
            ViewBag.SucessMessage = "Saved!";
            return(View(model));
        }