public async Task CreateKeywordAsync(KeywordDTO keywordDTO) { var keyword = _mapper.Map <Keyword>(keywordDTO); var resources = _mapper.Map <List <Resource> >(keywordDTO.Resources); keyword.KeywordResources = new List <KeywordResource>(); foreach (Resource resource in resources) { var existedResourse = await _resourceRepository.UrlExists(resource.UrlAddress); if (existedResourse != default) { _keywordRepository.AddResourceToKeyword(new KeywordResource { Resource = existedResourse, Keyword = keyword }); } else { _keywordRepository.AddResourceToKeyword(new KeywordResource { Resource = resource, Keyword = keyword }); } } _keywordRepository.Add(keyword); await _context.SaveChangesAsync(); }
public IActionResult Add([FromBody] KeywordDto keywordDto) { if (!ModelState.IsValid) { return(BadRequest()); } //For testing purposes: Keyword kwToAdd = new Keyword(); kwToAdd.kw = keywordDto.kw; int response = _kwRepo.Add(kwToAdd); if (response < 0) { return(Content("Fail.")); } else { return(Ok()); } }
public IActionResult AddKeyword(Keyword keyword) { var keywords = _keywordRepository.GetAll(); foreach (var item in keywords) { if (item.Title == keyword.Title) { ModelState.AddModelError("KeywordExist", "Already this Keyword is exist."); break; } } if (ModelState.IsValid) { _keywordRepository.Add(new Keyword() { Title = keyword.Title }); return(RedirectToAction("List")); } return(View(keyword)); }