public async Task CreateFavoriteLinkForUserAsync(ILink link, IGeneralUser user) { // Validate Link Link toBeCreatedFavoriteLink = _dbContext.Links.SingleOrDefault(l => l.Id == link.Id && l.IsDeleted == false ) ?? throw new ArgumentException($"Link ID: {link.Id} could not be found."); GeneralUser generalUser = _dbContext.GeneralUsers.SingleOrDefault(g => g.Id == user.Id) ?? throw new ArgumentException($"GeneralUser ID: {user.Id} could not be found."); if (toBeCreatedFavoriteLink.GeneralUserId == generalUser.Id) { throw new ArgumentException("Link belong to the logged in user."); } if (CheckExistingFavoriteLinkAysnc(toBeCreatedFavoriteLink.Id, generalUser.Id) != default) { throw new ArgumentException($"Favorite link already exists."); } // Save to db FavoriteLink newFavoriteLink = new FavoriteLink { Link = toBeCreatedFavoriteLink, GeneralUser = generalUser }; generalUser.FavoriteLinks.Add(newFavoriteLink); await _dbContext.SaveChangesAsync(); }
/// <summary> /// FindByIdentityAsync /// </summary> /// <param name="user"></param> /// <returns></returns> public async Task <IGeneralUser> FindByIdentityAsync(ClaimsPrincipal user) { var appUserId = _userManager.GetUserId(user); var generalUser = _dbContext.GeneralUsers .Include(g => g.Identity) .Include(g => g.City) .Include(g => g.GeneralUserTeachingAgeGroups) .ThenInclude(t => t.TeachingAgeGroup) .Include(g => g.GeneralUserTeachingSubjects) .ThenInclude(t => t.TeachingSubject) .Include(g => g.GeneralUserTeachingLevels) .ThenInclude(t => t.TeachingLevel) .Include(g => g.GeneralUserInterests) .ThenInclude(t => t.Interest) .FirstOrDefault(g => g.Identity.Id == new Guid(appUserId)); IGeneralUser IGeneralUser = await Task.FromResult(_mapper.Map <GeneralUser, IGeneralUser>(generalUser)); // Make call to AWS S3 to see if any profile image is linked to this GeneralUser if (IGeneralUser != null) { IImage profileImage = await _awsS3Service.GetProfileImageByUserAsync(appUserId); if (profileImage != null) { IGeneralUser.ProfileImage = profileImage; } } return(IGeneralUser); }
public async Task EditBuddyRequestAsync(ClaimsPrincipal user, int requestingGeneralUserId, bool acceptBuddyRequest) { BuddyRequestState state = acceptBuddyRequest ? BuddyRequestState.Matched : BuddyRequestState.Rejected; IGeneralUser generalUser = await _generalUsersService.FindByIdentityAsync(user); await _buddiesRepository.EditBuddyRequestAsync(generalUser.Id, requestingGeneralUserId, state); }
/// <summary> /// Lampen 5, interesse 3 per, ageGroup 2. schoollevel 2, location 2 filters toepassen /// </summary> /// <returns></returns> public async Task <IEnumerable <IFeed> > GetFeedsAsync(ClaimsPrincipal user) { IGeneralUser loggedInGeneralUser = await _generalUsersService.FindByIdentityAsync(user); IEnumerable <IGeneralUser> feedUsers = await _generalUsersRepository.FindWithSimilarPreferenceAsync(loggedInGeneralUser, 1); IEnumerable <ILink> feedLinks = await _linksRepository.FindWithSimilarPreferenceAsync(loggedInGeneralUser, 4); List <IFeed> feeds = new List <IFeed>(); var MatchedGeneralUser = feedUsers.FirstOrDefault(); if (MatchedGeneralUser != default) { ILimitedGeneralUser MatchedGeneralUserResponse = _mapper.Map <ILimitedGeneralUser>(MatchedGeneralUser); feeds.Add(new Feed { Type = FeedType.GeneralUser, Body = MatchedGeneralUserResponse }); } foreach (ILink link in feedLinks) { var linkResponse = _mapper.Map <GetLinkResponse>(link); feeds.Add(new Feed { Type = FeedType.Link, Body = linkResponse }); } return(feeds); }
public CreateGeneralUserResponse(IGeneralUser generalUser, JwtToken jwt) { if (generalUser == null || jwt == null) { return; } UserName = generalUser.Email; FirstName = generalUser.FirstName; LastName = generalUser.LastName; AuthToken = jwt.AuthToken; ExpiresIn = jwt.ExpiresIn; }
public async Task <IEnumerable <ILink> > GetFavoriteLinksByUserAsync(IGeneralUser generalUser) { IEnumerable <Link> favoriteLinks = _dbContext.FavoriteLinks .Where(fl => fl.GeneralUserId == generalUser.Id ) .Select(fl => fl.Link) // Include all related data of ReceivingGeneralUser .ToList(); IEnumerable <ILink> links = _dbContext.Links .Where( l => favoriteLinks.Contains(l) && l.IsDeleted == false ) // Include all related data of RequestingGeneralUser .Include(l => l.GeneralUser) .ThenInclude(g => g.Identity) .Include(l => l.GeneralUser.City) .Include(l => l.GeneralUser.GeneralUserTeachingAgeGroups) .ThenInclude(g => g.TeachingAgeGroup) .Include(l => l.GeneralUser.GeneralUserTeachingSubjects) .ThenInclude(g => g.TeachingSubject) .Include(l => l.GeneralUser.GeneralUserTeachingLevels) .ThenInclude(g => g.TeachingLevel) .Include(l => l.GeneralUser.GeneralUserInterests) .ThenInclude(g => g.Interest) .Include(l => l.LinkInterests) .ThenInclude(l => l.Interest) .Select(l => _mapper.Map <ILink>(l)) .ToList(); // Make call to AWS S3 to see if any profile image is linked to this GeneralUser foreach (ILink link in links) { link.Image = await LoadLinkImage(link); // Make call to AWS S3 to see if any profile image is linked to the GeneralUser of this link IImage profileImage = await _awsS3Service.GetProfileImageByUserAsync(link.GeneralUser.IdentityId); if (profileImage != null) { link.GeneralUser.ProfileImage = profileImage; } } return(await Task.FromResult(links)); }
public async Task DeleteFavoriteLinkForUserByIdAsync(int linkId, IGeneralUser generalUser) { FavoriteLink favoriteLink = _dbContext.FavoriteLinks .SingleOrDefault(l => l.GeneralUserId == generalUser.Id && l.LinkId == linkId ); if (favoriteLink == default) { throw new ArgumentException($"Favorite Link ID: {linkId} could not be found."); } _dbContext.FavoriteLinks.Remove(favoriteLink); await _dbContext.SaveChangesAsync(); }
public async Task <JsonResponse> GetMeAsync() { try { IGeneralUser generalUser = await _generalUsersService.FindByIdentityAsync(base.User); var response = _mapper.Map <IGeneralUser, GetGeneralUserResponse>(generalUser); return(new JsonResponse(response, HttpStatusCode.OK)); } catch (Exception ex) { await _logger.LogErrorAsync(ex.Message, ex); return(new JsonResponse(ex.Message, HttpStatusCode.BadRequest)); } }
public async Task DeleteLinkForUserByIdAsync(int linkId, IGeneralUser generalUser) { Link link = _dbContext.Links .SingleOrDefault(l => l.GeneralUserId == generalUser.Id && l.IsDeleted == false && l.Id == linkId ); if (link == default) { throw new ArgumentException($"Link ID: {linkId} could not be found."); } link.IsDeleted = true; await _dbContext.SaveChangesAsync(); }
public async Task <dynamic> FindByIdAsync(int generalUserId, ClaimsPrincipal user) { IGeneralUser loggedInUser = await FindByIdentityAsync(user); bool isBuddy = await _buddiesRepository.IsMatchedBuddyAsync(loggedInUser.Id, generalUserId); IGeneralUser IGeneralUser = await _generalUsersRepository.FindByIdAsync(generalUserId, user); if (isBuddy) { return(IGeneralUser); } else { return(_mapper.Map <IGeneralUser, ILimitedGeneralUser>(IGeneralUser)); } }
public async Task <IEnumerable <ILink> > FindWithSimilarPreferenceAsync(IGeneralUser loggedInUser, int limit) { IEnumerable <ILink> links = _dbContext.Links .Where(x => // Not links posted by user self x.GeneralUserId != loggedInUser.Id // Not deleted && x.IsDeleted == false // Not already saved as favorite link && !x.FavoriteLinks.Select(f => f.GeneralUserId).Contains(loggedInUser.Id) // Not those already seen && !x.GeneralUserSeenLinks.Select(g => g.GeneralUserId).Contains(loggedInUser.Id) ) .Include(l => l.LinkInterests) .ThenInclude(l => l.Interest) .Include(l => l.GeneralUser) .ThenInclude(g => g.Identity) .AsEnumerable() // ordering .OrderByDescending(d => d.LinkInterests.Select(l => l.Interest).Where(i => loggedInUser.Interests.Select(x => x.Id).Contains(i.Id)).Count()) .Take(limit) .Select(x => _mapper.Map <ILink>(x)) .ToList(); // Make call to AWS S3 to see if any profile image is linked to this GeneralUser foreach (ILink ILink in links) { ILink.Image = await LoadLinkImage(ILink); // Make call to AWS S3 to see if any profile image is linked to the GeneralUser of this link IImage profileImage = await _awsS3Service.GetProfileImageByUserAsync(ILink.GeneralUser.IdentityId); if (profileImage != null) { ILink.GeneralUser.ProfileImage = profileImage; } // flag as seen //_dbContext.GeneralUserSeenLink.Add(new GeneralUserSeenLink { GeneralUserId = loggedInUser.Id, LinkId = ILink.Id }); //_dbContext.SaveChanges(); } return(await Task.FromResult(links)); }
public async Task <IEnumerable <ILink> > GetLinksByUserAsync(IGeneralUser generalUser) { IEnumerable <ILink> links = _dbContext.Links .Where(link => link.GeneralUserId == generalUser.Id && link.IsDeleted == false ) .Include(link => link.GeneralUser) .Include(l => l.LinkInterests) .ThenInclude(l => l.Interest) .Select(link => _mapper.Map <ILink>(link)) .ToList(); // Make call to AWS S3 to see if any profile image is linked to this GeneralUser foreach (ILink link in links) { link.Image = await LoadLinkImage(link); } return(await Task.FromResult(links)); }
public async Task <IEnumerable <ILimitedGeneralUser> > GetMyBuddyRequestsAsync(ClaimsPrincipal user) { IGeneralUser generalUser = await _generalUsersService.FindByIdentityAsync(user); return(await _buddiesRepository.GetPendingBuddyRequestsAsync(generalUser.Id)); }
public async Task SendBuddyRequestAsync(ClaimsPrincipal user, int receivingGeneralUserId) { IGeneralUser requestingGeneralUser = await _generalUsersService.FindByIdentityAsync(user); await _buddiesRepository.CreateBuddyRequestAsync(requestingGeneralUser.Id, receivingGeneralUserId); }
public async Task <bool> IsMatchedBuddyAsync(ClaimsPrincipal user, int generalUserId) { IGeneralUser loggedInGeneralUser = await _generalUsersService.FindByIdentityAsync(user); return(await _buddiesRepository.IsMatchedBuddyAsync(loggedInGeneralUser.Id, generalUserId)); }
public async Task <IEnumerable <IGeneralUser> > GetMyBuddiesAsync(ClaimsPrincipal user) { IGeneralUser generalUser = await _generalUsersService.FindByIdentityAsync(user); return(await _buddiesRepository.GetMatchedBuddiesAsync(generalUser.Id)); }
public async Task <IEnumerable <ILink> > GetMyFavoriteLinksAsync(ClaimsPrincipal user) { IGeneralUser generalUser = await _generalUsersService.FindByIdentityAsync(user); return(await _linkRepository.GetFavoriteLinksByUserAsync(generalUser)); }
public async Task <ILink> CreateLinkForUserAsync(ILink toBeCreatedLink, IGeneralUser user) { using (_dbContext) { var strategy = _dbContext.Database.CreateExecutionStrategy(); ILink ILink = default; await strategy.Execute(async() => { using (TransactionScope transaction = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled)) { Link dbLink = _mapper.Map <ILink, Link>(toBeCreatedLink); if (dbLink == default) { throw new ArgumentNullException(nameof(dbLink)); } // Validate and save Interests if (toBeCreatedLink.Interests != null) { foreach (IInterest iInterest in toBeCreatedLink.Interests) { Interest interest = _dbContext.Interests.SingleOrDefault(t => t.Id == iInterest.Id); if (interest == default) { throw new ArgumentException($"Interest ID: {iInterest.Id} could not be found."); } LinkInterest newLinkInterest = new LinkInterest { InterestId = iInterest.Id }; dbLink.LinkInterests.Add(newLinkInterest); } } // Empty accidentally entered ImageWebUrl if uploadImageType is base64 if (dbLink.ImageType == UploadImageType.Base64 && dbLink.ImageWebUrl != null) { dbLink.ImageWebUrl = null; } // Set User dbLink.GeneralUserId = user.Id; // Prefix url string[] httpPrefixes = { "http://", "https://" }; bool prefixMatch = httpPrefixes.Any(prefix => dbLink.Url.StartsWith(prefix)); if (!prefixMatch) { dbLink.Url = "https://" + dbLink.Url; } // Save _dbContext.Links.Add(dbLink); await _dbContext.SaveChangesAsync(); ILink = _mapper.Map <Link, ILink>(dbLink); // save Image to AWS S3 if (toBeCreatedLink.ImageType == UploadImageType.Base64 && toBeCreatedLink.Image != null) { IImage linkImage = await _awsS3Service.UploadLinkImageAsync(toBeCreatedLink.Image, ILink); ILink.Image = linkImage; } // Commit transaction if all commands succeed, transaction will auto-rollback // when disposed if either commands fails transaction.Complete(); } }); return(ILink); } }
/// <summary> /// /// </summary> /// <param name="user"></param> /// <returns></returns> public async Task <IEnumerable <IGeneralUser> > FindWithSimilarPreferenceAsync(IGeneralUser loggedInUser, int limit) { IEnumerable <IGeneralUser> existingUserRequestsUsers = await _buddiesRepository.GetBuddyRequestedUsersByStateAsync(loggedInUser.Id); IEnumerable <int> existingUserRequestsUsersIds = existingUserRequestsUsers.Select(x => x.Id); Dictionary <int, int[]> techLevelMatchingTable = new Dictionary <int, int[]>(); techLevelMatchingTable.Add(1, new int[] { 3, 4, (5), (2) }); techLevelMatchingTable.Add(2, new int[] { 3, (4), (5), (1) }); techLevelMatchingTable.Add(3, new int[] { 4, 5, (1), (2) }); techLevelMatchingTable.Add(4, new int[] { 5, (3), (2), (1) }); techLevelMatchingTable.Add(5, new int[] { 4, 3, (2), (1) }); int[] techLevelMatchingSequence = techLevelMatchingTable[loggedInUser.TechLevel]; IEnumerable <IGeneralUser> matchedUsers = _dbContext.GeneralUsers .Where(x => // Match techlevel pre order techLevelMatchingSequence.Contains(x.TechLevel) // Not already have a buddy request initiated && !existingUserRequestsUsersIds.Contains(x.Id) // Not those already seen && !x.HasSeenGeneralUsers.Select(h => h.LoggedInGeneralUserId).Contains(loggedInUser.Id) ) .Include(x => x.Identity) .Include(x => x.City) .Include(x => x.GeneralUserTeachingAgeGroups) .ThenInclude(t => t.TeachingAgeGroup) .Include(x => x.GeneralUserTeachingSubjects) .ThenInclude(t => t.TeachingSubject) .Include(x => x.GeneralUserTeachingLevels) .ThenInclude(t => t.TeachingLevel) .Include(x => x.GeneralUserInterests) .ThenInclude(t => t.Interest) .AsEnumerable() // ordering .OrderBy(d => techLevelMatchingSequence.IndexOf(d.TechLevel)) .ThenByDescending(d => d.GeneralUserInterests.Select(g => g.Interest).Where(i => loggedInUser.Interests.Select(x => x.Id).Contains(i.Id)).Count()) .ThenByDescending(d => d.City.Id == loggedInUser.City.Id) .ThenByDescending(d => d.GeneralUserTeachingAgeGroups.Select(g => g.TeachingAgeGroup).Where(i => loggedInUser.TeachingAgeGroups.Select(x => x.Id).Contains(i.Id)).Count()) .ThenByDescending(d => d.GeneralUserTeachingLevels.Select(g => g.TeachingLevel).Where(i => loggedInUser.TeachingLevels.Select(x => x.Id).Contains(i.Id)).Count()) .Take(limit) .Select(x => _mapper.Map <IGeneralUser>(x)) .ToList(); // Flag as seen users foreach (IGeneralUser IGeneralUser in matchedUsers) { // Make call to AWS S3 to see if any profile image is linked to this GeneralUser IImage profileImage = await _awsS3Service.GetProfileImageByUserAsync(IGeneralUser.IdentityId); if (profileImage != null) { IGeneralUser.ProfileImage = profileImage; } // Flag as seen //_dbContext.GeneralUserSeenGeneralUser.Add(new GeneralUserSeenGeneralUser { LoggedInGeneralUserId = loggedInUser.Id, HasSeenGeneralUserId = IGeneralUser.Id }); //_dbContext.SaveChanges(); } return(matchedUsers); }
/// <summary> /// CreateGeneralUserAsync /// </summary> /// <param name="toBeCreatedgeneralUser"></param> /// <returns></returns> public async Task <IGeneralUser> CreateGeneralUserAsync(IGeneralUser toBeCreatedgeneralUser) { var strategy = _dbContext.Database.CreateExecutionStrategy(); IGeneralUser IGeneralUser = default; await strategy.Execute(async() => { using (TransactionScope transaction = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled)) { // First try to create AppUser var userIdentity = _mapper.Map <IGeneralUser, AppUser>(toBeCreatedgeneralUser); var createAppUserResult = await _userManager.CreateAsync(userIdentity, toBeCreatedgeneralUser.Password); if (!createAppUserResult.Succeeded) { throw new ArgumentException(createAppUserResult.Errors.FirstOrDefault()?.Description); } // Validate related id's var newGeneralUser = new GeneralUser { Identity = userIdentity, TechLevel = toBeCreatedgeneralUser.TechLevel, Location = toBeCreatedgeneralUser.Location, Locale = toBeCreatedgeneralUser.Locale, Gender = toBeCreatedgeneralUser.Gender }; // TODO: simplify mapping mechanism so that these relational objects don't need to be saved separately // Validate and save city City city = _dbContext.Cities.SingleOrDefault(c => c.Id == toBeCreatedgeneralUser.City.Id); newGeneralUser.City = city ?? throw new ArgumentException($"City ID: {toBeCreatedgeneralUser.City.Id} could not be found."); // Validate and save TeachingAgeGroups if (toBeCreatedgeneralUser.TeachingAgeGroups != null) { foreach (ITeachingAgeGroup iTeachingAgeGroup in toBeCreatedgeneralUser.TeachingAgeGroups) { TeachingAgeGroup teachingAgeGroup = _dbContext.TeachingAgeGroups.SingleOrDefault(t => t.Id == iTeachingAgeGroup.Id); if (teachingAgeGroup == default) { throw new ArgumentException($"TeachingGroup ID: {iTeachingAgeGroup.Id} could not be found."); } GeneralUserTeachingAgeGroup newGeneralUserTeachingAgeGroup = new GeneralUserTeachingAgeGroup { TeachingAgeGroupId = iTeachingAgeGroup.Id }; newGeneralUser.GeneralUserTeachingAgeGroups.Add(newGeneralUserTeachingAgeGroup); } } // Validate and Save TeachingSubjects if (toBeCreatedgeneralUser.TeachingSubjects != null) { foreach (ITeachingSubject iTeachingSubject in toBeCreatedgeneralUser.TeachingSubjects) { TeachingSubject teachingSubject = _dbContext.TeachingSubjects.SingleOrDefault(t => t.Id == iTeachingSubject.Id); if (teachingSubject == default) { throw new ArgumentException($"TeachingSubject ID: {iTeachingSubject.Id} could not be found."); } GeneralUserTeachingSubject newGeneralUserTeachingSubject = new GeneralUserTeachingSubject { TeachingSubjectId = iTeachingSubject.Id }; newGeneralUser.GeneralUserTeachingSubjects.Add(newGeneralUserTeachingSubject); } } // Validate and Save TeachingLevels if (toBeCreatedgeneralUser.TeachingLevels != null) { foreach (ITeachingLevel iTeachingLevel in toBeCreatedgeneralUser.TeachingLevels) { TeachingLevel teachingLevel = _dbContext.TeachingLevels.SingleOrDefault(t => t.Id == iTeachingLevel.Id); if (teachingLevel == default) { throw new ArgumentException($"TeachingLevel ID: {iTeachingLevel.Id} could not be found."); } GeneralUserTeachingLevel newGeneralUserTeachingLevel = new GeneralUserTeachingLevel { TeachingLevelId = iTeachingLevel.Id }; newGeneralUser.GeneralUserTeachingLevels.Add(newGeneralUserTeachingLevel); } } // Validate and Save Interests if (toBeCreatedgeneralUser.Interests != null) { foreach (IInterest iInterest in toBeCreatedgeneralUser.Interests) { Interest interest = _dbContext.Interests.SingleOrDefault(i => i.Id == iInterest.Id); if (interest == default) { throw new ArgumentException($"Interest ID: {iInterest.Id} could not be found."); } GeneralUserInterest newGeneralUserInterest = new GeneralUserInterest { InterestId = iInterest.Id }; newGeneralUser.GeneralUserInterests.Add(newGeneralUserInterest); } } await _dbContext.GeneralUsers.AddAsync(newGeneralUser); await _dbContext.SaveChangesAsync(); IGeneralUser = await Task.FromResult(_mapper.Map <GeneralUser, IGeneralUser>(newGeneralUser)); // Upload profile image to S3 only after succesfully AppUser creation if (toBeCreatedgeneralUser.ProfileImage != null) { IImage profileImage = await _awsS3Service.UploadProfileImageByUserAsync(toBeCreatedgeneralUser.ProfileImage, userIdentity.Id.ToString()); IGeneralUser.ProfileImage = profileImage; } // Commit transaction if all commands succeed, transaction will auto-rollback // when disposed if either commands fails transaction.Complete(); } }); return(IGeneralUser); }
public Task <IGeneralUser> CreateGeneralUserAsync(IGeneralUser generalUser) { return(_generalUsersRepository.CreateGeneralUserAsync(generalUser)); }
public HomeController(ILogger <HomeController> logger, IGeneralUser user) { _logger = logger; this.user = user; }
public async Task CreateFavoriteLinkAsync(ILink link, ClaimsPrincipal user) { IGeneralUser generalUser = await _generalUsersService.FindByIdentityAsync(user); await _linkRepository.CreateFavoriteLinkForUserAsync(link, generalUser); }
public GetGeneralUserResponse(IGeneralUser generalUser) { }
public async Task DeleteMyFavoriteLinkByIdAsync(int linkId, ClaimsPrincipal user) { IGeneralUser generalUser = await _generalUsersService.FindByIdentityAsync(user); await _linkRepository.DeleteFavoriteLinkForUserByIdAsync(linkId, generalUser); }
public GetNoLinksGeneralUserResponse(IGeneralUser generalUser) { }