コード例 #1
0
        public ActionResult CreateStep1(string userName)
        {
            ViewBag.MaximumSize = Services.SystemConfig.GetValueByKey(TeleConsult.Infrastructure.Core.Const.SystemConfig.FILE_UPLOAD_LIMITS);
            UserDto account = Services.Users.GetByUserName(userName);
            if (account != null)
            {
                LocationDto customerLocation = account.Locations.FirstOrDefault();
                LocationViewModel locationModel;
                if (customerLocation == null)
                {
                    locationModel = new LocationViewModel();
                }
                else
                {
                    Mapper.CreateMap<LocationDto, LocationViewModel>()
                        .ForMember(dest => dest.Id, opt => opt.MapFrom(src => src.Id));
                    locationModel = Mapper.Map<LocationDto, LocationViewModel>(customerLocation);
                }

                ProfileViewModel profileModel;
                ProfileDto customerProfile = account.Profiles.FirstOrDefault(x => x.UserId == account.Id);
                if (customerProfile == null)
                {
                    profileModel = new ProfileViewModel();
                }
                else
                {
                    Mapper.CreateMap<ProfileDto, ProfileViewModel>()
                        .ForMember(dest => dest.Id, opt => opt.MapFrom(src => src.Id));
                    profileModel = Mapper.Map<ProfileDto, ProfileViewModel>(customerProfile);
                }

                UserViewModel userModel;
                Mapper.CreateMap<UserDto, UserViewModel>()
                    .ForMember(dest => dest.Id, opt => opt.MapFrom(src => src.Id));
                userModel = Mapper.Map<UserDto, UserViewModel>(account);
                userModel.TitleGender = account.Title;
                userModel.AvatarPath = account.Avatar != null ? Path.Combine(BlueChilli.Web.Helpers.S3PathWithoutQuery("").Substring(1),
                    account.Avatar)
                : ConstPath.DefaulAvatar;

                var model = new ProfileIndexViewModel { Location = locationModel, Profile = profileModel, User = userModel };
                model.Location.ListCountry = GetCultureList();
                model.Location.ListTimeZone = GetTimeZoneList();
                return View(model);
            }
            else
            {
                return View("Error");
            }
        }
コード例 #2
0
        public ActionResult CreateStep1(ProfileIndexViewModel model)
        {
            ViewBag.MaximumSize = Services.SystemConfig.GetValueByKey(TeleConsult.Infrastructure.Core.Const.SystemConfig.FILE_UPLOAD_LIMITS);
            if (model == null)
            {
                model = new ProfileIndexViewModel();
                return View("CreateStep1", model);
            }
            if (ModelState.IsValid)
            {
                UserDto account = Services.Users.GetByUserName(model.User.UserName);

                if (account != null)
                {
                    Mapper.CreateMap<LocationViewModel, LocationDto>()
                        .ForMember(dest => dest.Id, opt => opt.MapFrom(src => src.Id));

                    LocationDto customerLocation = account.Locations.FirstOrDefault();
                    if (customerLocation == null)
                    {
                        customerLocation = new LocationDto();
                        customerLocation = Mapper.Map<LocationViewModel, LocationDto>(model.Location);
                        customerLocation.CreatedDate = DateTime.UtcNow;
                        customerLocation.ModifiedDate = DateTime.UtcNow;
                        account.Locations.Add(customerLocation);
                    }
                    else
                    {
                        customerLocation = Mapper.Map<LocationViewModel, LocationDto>(model.Location);
                        customerLocation.CreatedDate = DateTime.UtcNow;
                        customerLocation.ModifiedDate = DateTime.UtcNow;
                        account.Locations[0] = customerLocation;
                    }

                    account.Title = model.User.TitleGender;
                    account.FirstName = model.User.FirstName;
                    account.LastName = model.User.LastName;

                    ProfileDto customerProfile = account.Profiles.FirstOrDefault(x => x.UserId == account.Id);
                    Mapper.CreateMap<ProfileViewModel, ProfileDto>()
                            .ForMember(dest => dest.Id, opt => opt.MapFrom(src => src.Id));

                    //if (customerProfile == null && model.Profile != null)
                    //{
                    //    customerProfile = new ProfileDto();
                    //    customerProfile = Mapper.Map<ProfileViewModel, ProfileDto>(model.Profile);
                    //    account.Profiles.Add(customerProfile);
                    //}
                    //else if (customerProfile != null)
                    //{
                    //    customerProfile = Mapper.Map<ProfileViewModel, ProfileDto>(model.Profile);
                    //    customerProfile.CreatedDate = DateTime.UtcNow;
                    //    customerProfile.ModifiedDate = DateTime.UtcNow;
                    //}

                    // Avatar
                    HttpPostedFileBase image = Request.Files["uploadAvatar"];
                    if (image != null && image.ContentType.Contains("image"))
                    {
                        var byteImageArray = ConvertImageToByteArray(System.Drawing.Image.FromStream(image.InputStream, true, true));
                        string fileExtension = ConfigurationManager.AppSettings["avatarFileExtension"];
                        var filename = account.UserName + fileExtension;
                        var s3Root = BlueChilli.Web.Helpers.S3PathWithoutQuery("").Substring(1);

                        try
                        {
                            var savedFileName = Services.Image.SaveImage(filename, byteImageArray, "Avatars");
                            account.Avatar = savedFileName; // Save direction image
                        }
                        catch (Exception ex)
                        {
                            Log.Error("CreateStep1 - Save Image to S3: " + ex.ToString());
                            if (account.Avatar == null)
                            {
                                account.Avatar = ConstPath.DefaulAvatar; // Save default direction image
                            }
                        }
                    }
                    else // Default image
                    {
                        if (account.Avatar == null)
                        {
                            account.Avatar = ConstPath.DefaulAvatar;
                        }
                    }
                    account.CompletedStep = Step.Step1;
                    account.ModifiedDate = DateTime.Now;
                    account.ExpiredDate = DateTime.Now.AddDays(7);

                    if (Services.Users.UpdateUserInfo(account))
                    {
                        return RedirectToAction("CreateStep2", new { userName = account.UserName });
                    }
                    else
                    {
                        ViewBag.ErrorMessage = "Update account failed";
                    }
                }
                else
                {
                    ViewBag.ErrorMessage = "Account does not exists";
                }
            }

            return View("CreateStep1", model);
        }