Beispiel #1
0
        private async Task <Service> MappingService(PostServiceViewModel vm, string currentUserContext)
        {
            Service sv = new Service();

            sv.CategoryId     = vm.CategoryId;
            sv.DateCreated    = DateTime.Now;
            sv.PriceOfService = vm.PriceOfService;
            sv.Description    = vm.Description;
            sv.ServiceName    = vm.ServiceName;
            sv.codeConfirm    = RandomCodeSupport.RandomString(6);
            sv.Status         = (await _getPermissionActionQuery.ExecuteAsync(currentUserContext, ConstantFunctions.SERVICE, ActionSetting.CanCreate) ||
                                 await _checkUserIsAdminQuery.ExecuteAsync(currentUserContext)) ? Status.WaitingApprove : Status.Pending;
            sv.ServiceImages = vm.listImages.Select(x => new ServiceImage
            {
                Path        = x.Path != null ? x.Path : "",
                DateCreated = DateTime.Now,
                isAvatar    = x.IsAvatar
            }).ToList();

            sv.TagServices = vm.tagofServices.Where(x => x.isDelete == false && x.isAdd == false).Select(x => new Model.Entities.ServiceModel.TagService
            {
                TagId = Guid.Parse(x.TagId),
            }).ToList();
            return(sv);
        }
Beispiel #2
0
        private async Task <PostServiceViewModel> MapViewModel(
            Service serv, AppUser user, Provider provider)
        {
            var query = await _serviceRepository.FindAllAsync();

            var providerService = await _providerServiceRepository.FindAllAsync();

            var userService = await _userServiceRepository.FindAllAsync();

            var providers = await _providerRepository.FindAllAsync();


            var getTag = await _tagRepository.FindAllAsync();

            var getUserTag = await _tagServiceRepository.FindAllAsync(x => x.ServiceId == serv.Id);

            var getListTag = (from tag in getTag.ToList()
                              join serviceTag in getUserTag.ToList()
                              on tag.Id equals serviceTag.TagId
                              select new
            {
                tag.TagName
            }).ToList();

            var getImage = await _imageRepository.FindAllAsync(x => x.ServiceId == serv.Id);

            PostServiceViewModel postServiceView = new PostServiceViewModel();

            postServiceView.Id         = serv.Id.ToString();
            postServiceView.listImages = getImage.Select(x => new PostServiceImageViewModel
            {
                Path    = x.Path,
                ImageId = x.Id
            }).ToList();
            postServiceView.PriceOfService = serv.PriceOfService;
            postServiceView.CategoryName   = _getByIDCategoryServiceQuery.ExecuteAsync(serv.CategoryId).Result.CategoryName;
            postServiceView.ServiceName    = serv.ServiceName;
            postServiceView.Status         = serv.Status;
            postServiceView.tagofServices  = getListTag.Select(x => new TagofServiceViewModel
            {
                TagName = x.TagName
            }).ToList();
            postServiceView.Description = serv.Description;
            postServiceView.ProviderId  = _getProviderInformationQuery.ExecuteAsync(serv.Id, query, providers, providerService).idProvider
                                          == "" ? _getUserInformationQuery.ExecuteAsync(serv.Id, query, userService) :
                                          _getProviderInformationQuery.ExecuteAsync(serv.Id, query, providers, providerService).idProvider;
            postServiceView.CategoryId = serv.CategoryId;
            postServiceView.Author     =
                _getProviderInformationQuery.ExecuteAsync(serv.Id, query, providers, providerService).NameProvider
                == "" ? _getUserInformationQuery.ExecuteAsync(serv.Id, query, userService) :
                _getProviderInformationQuery.ExecuteAsync(serv.Id, query, providers, providerService).NameProvider;
            return(postServiceView);
        }
        public async Task <PostServiceViewModel> ExecuteAsync(PostServiceViewModel model)
        {
            try
            {
                await _elasticSearchRepository.DeleteAsync(model);

                return(model);
            }
            catch (Exception ex)
            {
                await Logging <DeleteService> .ErrorAsync(ex, ActionCommand.COMMAND_DELETE, "System", "Has error");

                return(null);
            }
        }
Beispiel #4
0
 private async Task <Service> MappingService(PostServiceViewModel vm, Service sv, string currentUserContext)
 {
     sv.CategoryId     = vm.CategoryId;
     sv.DateModified   = DateTime.Now;
     sv.PriceOfService = vm.PriceOfService;
     sv.Description    = vm.Description;
     sv.ServiceName    = vm.ServiceName;
     sv.Status         = (await _getPermissionActionQuery.ExecuteAsync(currentUserContext, ConstantFunctions.SERVICE, ActionSetting.CanCreate) ||
                          await _checkUserIsAdminQuery.ExecuteAsync(currentUserContext)) ? Status.WaitingApprove : Status.Pending;
     sv.TagServices = vm.tagofServices.Where(t => t.isDelete == false && t.isAdd == false).Select(x => new Model.Entities.ServiceModel.TagService
     {
         TagId = Guid.Parse(x.TagId),
     }).ToList();
     return(sv);
 }
Beispiel #5
0
        public async Task <PostServiceViewModel> ExecuteAsync(PostServiceViewModel postService)
        {
            try
            {
                await _elasticSearchRepository.SaveSingleAsync(postService);

                return(postService);
            }
            catch (Exception ex)
            {
                await Logging <AddNewService> .ErrorAsync(ex, ActionCommand.COMMAND_ADD, "System", "Has error");

                throw;
            }
        }
Beispiel #6
0
        public async Task <IActionResult> updatePostService([FromBody] PostServiceViewModel vm)
        {
            var model = await _updatePostServiceCommand.ExecuteAsync(vm);

            return(new OkObjectResult(model));
        }
Beispiel #7
0
        public async Task <IActionResult> RegisterServiceFromUser([FromBody] PostServiceViewModel vm)
        {
            var model = await _registerServiceFromUserCommand.ExecuteAsync(vm);

            return(new OkObjectResult(model));
        }
Beispiel #8
0
        public async Task <IActionResult> RejectPostService([FromBody] PostServiceViewModel vm)
        {
            var model = await _rejectPostServiceCommand.ExecuteAsync(vm.Id, vm.Reason);

            return(new OkObjectResult(model));
        }
Beispiel #9
0
        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()
                });
            }
        }
Beispiel #10
0
        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()
                });
            }
        }