public async Task <CommandResult <PostServiceViewModel> > ExecuteAsync(PostServiceViewModel vm) { var userId = _httpContextAccessor.HttpContext.User.Identity.Name; var userName = _userManager.FindByIdAsync(userId).Result.UserName; try { //var checkUserIsAdmin = await _checkUserIsAdminQuery.ExecuteAsync(userId); if (await _getPermissionActionQuery.ExecuteAsync(userId, ConstantFunctions.SERVICE, ActionSetting.CanCreate) || await _checkUserIsAdminQuery.ExecuteAsync(userId)) { //Add new tag when isAdd equal true List <Tag> newTag = new List <Tag>(); foreach (var item in vm.tagofServices) { if (item.isAdd == true) { newTag.Add(new Tag { TagName = item.TagName }); } } await _tagServiceRepository.Add(newTag); //Mapping between ViewModel and Model of Service var mappingService = await MappingService(vm, userId); await _postServiceRepository.Add(mappingService); //Mapping between ViewModel and Model of UserService var mappingUserService = MappingUserService(mappingService.Id, !string.IsNullOrEmpty(vm.UserId) ? Guid.Parse(vm.UserId) : Guid.Parse(userId)); await _userServiceRepository.Add(mappingUserService); //Add new Tag with Id in TagService foreach (var item in newTag) { Model.Entities.ServiceModel.TagService mappingTag = new Model.Entities.ServiceModel.TagService(); mappingTag.TagId = item.Id; mappingService.TagServices.Add(mappingTag); } await _tagServiceRepository.SaveAsync(); //Write Log await Logging <RegisterServiceFromUserCommand> . InformationAsync(ActionCommand.COMMAND_ADD, userName, JsonConvert.SerializeObject(vm)); var findUserInformation = await _userManager.FindByIdAsync(vm.UserId); //Send mail for user if admin if ((await _getPermissionActionQuery.ExecuteAsync(userId, ConstantFunctions.SERVICE, ActionSetting.CanCreate) || await _checkUserIsAdminQuery.ExecuteAsync(userId))) { //Set content for email //Generate code //Create Generate code var generateCode = _configuration.GetSection("Host").GetSection("LinkConfirmService").Value + mappingService.codeConfirm + '_' + mappingService.Id; var getEmailContent = await _getAllEmailServiceQuery.ExecuteAsync(); var getFirstEmail = getEmailContent.Where(x => x.Name == EmailName.Approve_Service).FirstOrDefault(); getFirstEmail.Message = getFirstEmail.Message. Replace(EmailKey.UserNameKey, findUserInformation.Email). Replace(EmailKey.ConfirmLink, generateCode);; ContentEmail(_config.Value.SendGridKey, getFirstEmail.Subject, getFirstEmail.Message, _userManager.FindByIdAsync(!string.IsNullOrEmpty(vm.UserId) ? vm.UserId : userId).Result.Email).Wait(); } //End send mail for user return(new CommandResult <PostServiceViewModel> { isValid = true, myModel = new PostServiceViewModel { Id = mappingService.Id.ToString(), DateCreated = mappingService.DateCreated, IsProvider = false, Author = findUserInformation.UserName + "(" + findUserInformation.Email + ")", UserId = vm.UserId, AvtService = mappingService.ServiceImages.Where(x => x.isAvatar == true).FirstOrDefault().Path, listImages = mappingService.ServiceImages.Select(x => new PostServiceImageViewModel { ImageId = x.Id, IsAvatar = x.isAvatar, Path = x.Path }).ToList(), CategoryId = vm.CategoryId, CategoryName = vm.CategoryName, Description = vm.Description, PriceOfService = vm.PriceOfService, ServiceName = vm.ServiceName, Status = mappingService.Status, tagofServices = mappingService.TagServices.Select(x => new TagofServiceViewModel { TagId = x.TagId.ToString(), TagName = _tagServiceRepository.FindSingleAsync(t => t.Id == x.TagId).Result.TagName }).ToList(), } }); } else { await Logging <RegisterServiceFromUserCommand> . WarningAsync(ActionCommand.COMMAND_ADD, userName, ErrorMessageConstant.ERROR_ADD_PERMISSION); return(new CommandResult <PostServiceViewModel> { isValid = false, errorMessage = ErrorMessageConstant.ERROR_ADD_PERMISSION }); } } catch (System.Exception ex) { await Logging <RegisterServiceFromUserCommand> . ErrorAsync(ex, ActionCommand.COMMAND_ADD, userName, "Had error"); return(new CommandResult <PostServiceViewModel> { isValid = true, myModel = vm, errorMessage = ex.InnerException.ToString() }); } }
public async Task <CommandResult <PostServiceViewModel> > ExecuteAsync(PostServiceViewModel vm) { var userId = _httpContext.HttpContext.User.Identity.Name; var userName = _userManager.FindByIdAsync(userId).Result.UserName; try { var getPermissionForService = await IsOwnService(Guid.Parse(vm.Id)); if (getPermissionForService.isValid || await _checkUserIsAdminQuery.ExecuteAsync(userId) || await _getPermissionActionQuery.ExecuteAsync(userId, ConstantFunctions.SERVICE, ActionSetting.CanUpdate)) { var currentService = getPermissionForService.myModel; var service = await _serviceRepository.FindByIdAsync(Guid.Parse(vm.Id)); if (service == null) { return(new CommandResult <PostServiceViewModel> { isValid = false, errorMessage = ErrorMessageConstant.ERROR_CANNOT_FIND_ID }); } List <Tag> newTag = new List <Tag>(); var tagDelete = await RemoveTag(Guid.Parse(vm.Id), vm.tagofServices); foreach (var item in vm.tagofServices) { if (item.isAdd == true) { newTag.Add(new Tag { TagName = item.TagName }); } } //Update Image List <ServiceImage> lstService = new List <ServiceImage>(); foreach (var item in vm.listImages) { if (item.ImageId == 0) { ServiceImage serviceImage = new ServiceImage() { DateCreated = DateTime.Now, Path = item.Path, isAvatar = item.IsAvatar, ServiceId = Guid.Parse(vm.Id) }; lstService.Add(serviceImage); } else { var imageId = await _serviceImageRepository.FindByIdAsync(item.ImageId); if (imageId != null) { imageId.isAvatar = item.IsAvatar; imageId.DateModified = DateTime.Now; _serviceImageRepository.Update(imageId); } } } var deletImage = await RemoveImage(Guid.Parse(vm.Id), vm.listImages); _serviceImageRepository.RemoveMultiple(deletImage); await _tagServiceRepository.Add(newTag); _tagServiceRepository.RemoveMultiple(tagDelete); var mappingService = await MappingService(vm, service, userId); mappingService.TagServices = null; foreach (var tag in newTag) { Model.Entities.ServiceModel.TagService mappingTag = new Model.Entities.ServiceModel.TagService(); mappingTag.TagId = tag.Id; mappingTag.ServiceId = Guid.Parse(vm.Id); await _serviceOfTagRepository.Add(mappingTag); } _serviceRepository.Update(mappingService); await _serviceRepository.SaveAsync(); //Cotentemail var findEmailUser = await GetEmailUserAsync(mappingService); if (findEmailUser != ErrorMessageConstant.ERROR_CANNOT_FIND_ID) { //Set content for email //Get All email var getAllEmail = await _getAllEmailServiceQuery.ExecuteAsync(); var getFirstEmail = getAllEmail.Where(x => x.Name == EmailName.Approve_Service).FirstOrDefault(); var generateCode = _configuration.GetSection("Host").GetSection("LinkConfirmService").Value + mappingService.codeConfirm + '_' + mappingService.Id; getFirstEmail.Message = getFirstEmail.Message. Replace(EmailKey.ServiceNameKey, mappingService.ServiceName). Replace(EmailKey.UserNameKey, findEmailUser). Replace(EmailKey.ConfirmLink, generateCode); ContentEmail(_configEmail.Value.SendGridKey, getFirstEmail.Subject, getFirstEmail.Message, findEmailUser).Wait(); } else { await Logging <UpdatePostServiceCommand> . WarningAsync(ActionCommand.COMMAND_APPROVE, userName, "Cannot find email user"); return(new CommandResult <PostServiceViewModel> { isValid = false, errorMessage = "Cannot find email user" }); } await Logging <UpdatePostServiceCommand> . InformationAsync(ActionCommand.COMMAND_UPDATE, userName, JsonConvert.SerializeObject(vm)); return(new CommandResult <PostServiceViewModel> { isValid = true, }); } await Logging <UpdatePostServiceCommand> . WarningAsync(ActionCommand.COMMAND_UPDATE, userName, ErrorMessageConstant.ERROR_UPDATE_PERMISSION); return(new CommandResult <PostServiceViewModel> { isValid = false, errorMessage = ErrorMessageConstant.ERROR_UPDATE_PERMISSION }); } catch (System.Exception ex) { await Logging <UpdatePostServiceCommand> . ErrorAsync(ex, ActionCommand.COMMAND_UPDATE, userName, "Has error"); return(new CommandResult <PostServiceViewModel> { isValid = false, errorMessage = ex.InnerException.ToString() }); } }