public void ChangeIsDeletedToFalse_WhenDTOIsNotNullAndIsAlreadyAdded() { var stubSaver = new Mock <ISaver>(); var stubTwitterUsers = new Mock <IRepository <TwitterUser> >(); var stubUsers = new Mock <IRepository <User> >(); var stubUserTwitterUsers = new Mock <IRepository <UserTwitterUser> >(); var service = new UserService(stubSaver.Object, stubTwitterUsers.Object, stubUsers.Object, stubUserTwitterUsers.Object); var userDto = new TwitterUserDTO { TwitterUserId = "345", Name = "Zoro" }; var user = new TwitterUser { TwitterUserId = userDto.TwitterUserId, Name = userDto.Name }; //var users = new List<TwitterUser> { user }; //stubTwitterUsers.Setup(s => s.AllAndDeleted).Returns(users.AsQueryable()); //Act service.AddUser(userDto, "345"); //Assert stubSaver.Verify(v => v.SaveChanges(), Times.Once); }
public IActionResult RemoveUser(TwitterUserDTO twitterUser) { var aspUserId = this.userManager.GetUserId(HttpContext.User); this.userService.RemoveTwitterUser(twitterUser.TwitterUserId, aspUserId); return(Ok("")); }
public IActionResult AddUser(TwitterUserDTO user) { user = this.twitterApiService.FindTwitterUserByTwitterId(user.TwitterUserId); var aspUserId = this.userManager.GetUserId(HttpContext.User); this.userService.AddUser(user, aspUserId); return(this.Ok()); }
public void AddUser(TwitterUserDTO dto, string aspUserId) { if (aspUserId == null) { throw new ArgumentNullException("UserId cannot be null!"); } if (aspUserId == string.Empty) { throw new ArgumentException("UserId cannot be empty!"); } if (dto == null) { throw new ArgumentNullException("TwitterUser cannot be null!"); } var existingUserInDb = this.twitterUsers.All .FirstOrDefault(x => x.TwitterUserId == dto.TwitterUserId); if (existingUserInDb != null) { bool isAlreadyBeenSaved = this.userTwitterUsers.AllAndDeleted .Any(x => x.TwitterUserId == existingUserInDb.Id && x.UserId == aspUserId); if (isAlreadyBeenSaved) { var existingEntity = this.userTwitterUsers.AllAndDeleted .First(x => x.TwitterUserId == existingUserInDb.Id && x.UserId == aspUserId); existingEntity.IsDeleted = false; this.userTwitterUsers.Update(existingEntity); } else { existingUserInDb.UserTwitterUsers = new List <UserTwitterUser>(); existingUserInDb.UserTwitterUsers.Add(new UserTwitterUser() { UserId = aspUserId }); } } else { var model = new TwitterUser { TwitterUserId = dto.TwitterUserId, Name = dto.Name, FollowersCount = dto.FollowersCount, FriendsCount = dto.FriendsCount, Location = dto.Location, ImgUrl = dto.ImgUrl, Description = dto.Description, UserTwitterUsers = new List <UserTwitterUser>() }; User aspUser = this.aspUsers.All .FirstOrDefault(u => u.Id == aspUserId); model.UserTwitterUsers.Add(new UserTwitterUser() { User = aspUser }); this.twitterUsers.Add(model); } this.saver.SaveChanges(); }