public async Task <IResultModel> Add(TopicAddModel model) { var entity = _mapper.Map <TopicEntity>(model); //if (await _repository.Exists(entity)) //{ //return ResultModel.HasExists; //} using var uow = _forumDbContext.NewUnitOfWork(); var result = await _repository.AddAsync(entity, uow); if (result && model.Tags != null && model.Tags.Count() > 0) { var tagList = model.Tags.Select(s => new TopicTagEntity { TopicId = entity.Id, TagId = s }).ToList(); //新增只需要重新添加即可 消息队列处理 await _topicTagRepository.AddAsync(tagList, uow); uow.Commit(); await _tagRepository.AddCount(model.Tags); await _categoryRepository.AddCount(new int[] { entity.CategoryId }); } return(ResultModel.Result(result)); }
public async Task AddAsync(TopicAddModel model) { MultipartFormDataContent formData = new MultipartFormDataContent(); if (model.Image != null) { var stream = new MemoryStream(); await model.Image.CopyToAsync(stream); var bytes = stream.ToArray(); ByteArrayContent byteContent = new ByteArrayContent(bytes); byteContent.Headers.ContentType = new MediaTypeHeaderValue(model.Image.ContentType); formData.Add(byteContent, nameof(TopicUpdateModel.Image), model.Image.FileName); } var user = _httpContextAccessor.HttpContext.Session.GetObject <AppUserViewModel>("activeUser"); model.AppUserId = user.Id; formData.Add(new StringContent(model.AppUserId.ToString()), nameof(TopicAddModel.AppUserId)); formData.Add(new StringContent(model.Description.ToString()), nameof(TopicAddModel.Description)); formData.Add(new StringContent(model.ShortDescription.ToString()), nameof(TopicAddModel.ShortDescription)); formData.Add(new StringContent(model.Title.ToString()), nameof(TopicAddModel.Title)); _httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", _httpContextAccessor.HttpContext.Session.GetString("token")); await _httpClient.PostAsync("", formData); }
public async Task <IActionResult> AddTopic(TopicAddModel model) { TempData["active"] = "topic"; if (ModelState.IsValid) { await _topicApiService.AddAsync(model); return(RedirectToAction("Index")); } return(View(model)); }
public async Task <IActionResult> Create([FromForm] TopicAddModel topicAddModel) { var uploadModel = await UploadFileAsync(topicAddModel.Image, "image/jpeg"); if (uploadModel.UploadState == UploadState.Success) { topicAddModel.ImagePath = uploadModel.NewName; await _topicService.AddAsync(_mapper.Map <Topic>(topicAddModel)); return(Created("", topicAddModel)); } else if (uploadModel.UploadState == UploadState.NotExist) { await _topicService.AddAsync(_mapper.Map <Topic>(topicAddModel)); return(Created("", topicAddModel)); } else { return(BadRequest(uploadModel.ErrorMessage)); } }
public Task <IResultModel> Add(TopicAddModel model) { return(_service.Add(model)); }