public bool SaveUserTweeter(string userId, long tweeterId) { if (string.IsNullOrEmpty(userId)) { throw new ArgumentNullException(nameof(userId)); } if (tweeterId <= 0) { throw new ArgumentNullException(nameof(tweeterId)); } if (!this.DbContainsUserTweeter(userId, tweeterId)) { var userTweeter = new UserTweeter { UserId = userId, TweeterId = tweeterId }; this.unitOfWork.UsersTweeterRepository.Add(userTweeter); this.unitOfWork.SaveChanges(); return(true); } return(false); }
public void SaveUserTweeter(string userId, TweeterDTO tweeter) { if (string.IsNullOrEmpty(userId)) { throw new ArgumentNullException(nameof(userId)); } var assignTweeterToUser = this.unitOfWork.TweeterRepository.All().FirstOrDefault(x => x.Id == tweeter.Id); if (assignTweeterToUser == null) { assignTweeterToUser = this.tweeterService.CreateTweeter(tweeter); var userTweeter = new UserTweeter { UserId = userId, TweeterId = assignTweeterToUser.Id }; this.unitOfWork.UsersTweeterRepository.Add(userTweeter); this.unitOfWork.SaveChanges(); } else { if (assignTweeterToUser.IsDeleted) { assignTweeterToUser.IsDeleted = false; assignTweeterToUser.DeletedOn = null; this.unitOfWork.SaveChanges(); } if (!this.DbContainsUserTweeter(userId, tweeter.Id)) { var userTweeter = new UserTweeter { UserId = userId, TweeterId = assignTweeterToUser.Id }; this.unitOfWork.UsersTweeterRepository.Add(userTweeter); this.unitOfWork.SaveChanges(); } else { var restoreUserTweeter = this.unitOfWork.UsersTweeterRepository.All().FirstOrDefault(x => x.TweeterId == tweeter.Id && x.UserId == userId && x.IsDeleted); if (restoreUserTweeter != null) { restoreUserTweeter.IsDeleted = false; restoreUserTweeter.DeletedOn = null; this.unitOfWork.SaveChanges(); } } } }