public IHttpActionResult AddSearchCriteriaShortcut([FromBody] SearchCriteriaShortcutDto shortcutDto) { if (String.IsNullOrEmpty(shortcutDto.UniqueID)) { shortcutDto.UniqueID = Guid.NewGuid().ToString(); } _worklistService.AddSearchCriteriaShortcut(shortcutDto); var shorcut = _worklistService.GetSearchCriteriaShortcut(shortcutDto.UniqueID); return(Created("", shorcut)); }
public void AddSearchCriteriaShortcutTest() { var shortcutId = Guid.NewGuid().ToString(); var shortcutName = RandomHelper.GenerateString(); // add shortcut for this user var shortcutDto = new SearchCriteriaShortcutDto { UniqueID = shortcutId, Owner = _userId, Name = shortcutName, IsDefault = false, criteria = new WorklistSearchCriteriaDto() }; _worklistService.AddSearchCriteriaShortcut(shortcutDto); // this user's shortcut count should be 3, originally is 2 var result = _worklistService.GetSearchCriteriaShortcuts(_userId); Assert.AreEqual(3, result.Count()); Assert.IsTrue(result.Any(s => s.UniqueID == shortcutId)); // add shortcut for another user var userId = Guid.NewGuid().ToString(); shortcutDto = new SearchCriteriaShortcutDto { UniqueID = shortcutId, Owner = userId, Name = shortcutName, IsDefault = false, criteria = new WorklistSearchCriteriaDto() }; _worklistService.AddSearchCriteriaShortcut(shortcutDto); // this user's shortcut count is still 3 result = _worklistService.GetSearchCriteriaShortcuts(_userId); Assert.AreEqual(3, result.Count()); // another user's shortcut count is 1 result = _worklistService.GetSearchCriteriaShortcuts(userId); Assert.AreEqual(1, result.Count()); // add duplicated shorcut name for this user shortcutDto = new SearchCriteriaShortcutDto { UniqueID = shortcutId, Owner = _userId, Name = shortcutName, IsDefault = false, criteria = new WorklistSearchCriteriaDto() }; // except DuplicatedException _worklistService.AddSearchCriteriaShortcut(shortcutDto); }
public void AddSearchCriteriaShortcut(SearchCriteriaShortcutDto shortcut) { var existedShortcut = _dbContext.Set <Shortcut>().Where(s => s.Owner.Equals(shortcut.Owner, StringComparison.OrdinalIgnoreCase) && s.Category == WorklistService.Category && s.Name.Equals(shortcut.Name, StringComparison.OrdinalIgnoreCase)).Take(1).FirstOrDefault(); if (existedShortcut != null) { if (!shortcut.IgnoreNameDuplicated) { throw new DuplicateNameException(); } else { shortcut.IsDefault = existedShortcut.Type == 1; _shortcutRepository.Delete(existedShortcut); } } shortcut.Value = JsonSerializer <WorklistSearchCriteriaDto> .ToJson(shortcut.criteria); _shortcutRepository.Add(Mapper.Map <SearchCriteriaShortcutDto, Shortcut>(shortcut)); _shortcutRepository.SaveChanges(); }