From 376322db178d8e8b2bdd77b810999e80853ef761 Mon Sep 17 00:00:00 2001 From: Marc Vaccaro Date: Thu, 21 Feb 2019 23:48:59 -0800 Subject: [PATCH] Wire up like functionality on front-end --- .../member-card/member-card.component.html | 2 +- .../member-card/member-card.component.ts | 14 +++++++- Client/src/app/services/user.service.ts | 4 +++ Data/FriendRepository.cs | 33 +++++++++++++++++++ Helpers/UserParameters.cs | 2 ++ 5 files changed, 53 insertions(+), 2 deletions(-) diff --git a/Client/src/app/components/members/member-card/member-card.component.html b/Client/src/app/components/members/member-card/member-card.component.html index 631e845..f9ef5bb 100644 --- a/Client/src/app/components/members/member-card/member-card.component.html +++ b/Client/src/app/components/members/member-card/member-card.component.html @@ -6,7 +6,7 @@
  • - +
  • diff --git a/Client/src/app/components/members/member-card/member-card.component.ts b/Client/src/app/components/members/member-card/member-card.component.ts index 59e04b5..fa337b2 100644 --- a/Client/src/app/components/members/member-card/member-card.component.ts +++ b/Client/src/app/components/members/member-card/member-card.component.ts @@ -1,5 +1,8 @@ import { Component, OnInit, Input } from '@angular/core'; import { User } from 'src/app/models/user'; +import { AuthService } from 'src/app/services/auth.service'; +import { UserService } from 'src/app/services/user.service'; +import { AlertifyService } from 'src/app/services/alertify.service'; @Component({ selector: 'app-member-card', @@ -8,9 +11,18 @@ import { User } from 'src/app/models/user'; }) export class MemberCardComponent implements OnInit { @Input() user: User; - constructor() { } + + constructor(private authService: AuthService, private userService: UserService, private alertify: AlertifyService) { } ngOnInit() { } + likeUser(recipientId: number) { + this.userService.likeUser(this.authService.decodedToken.nameid, recipientId).subscribe(data => { + this.alertify.success(`${this.user.knownAs} was liked`); + }, error => { + this.alertify.error(error); + }); + } + } diff --git a/Client/src/app/services/user.service.ts b/Client/src/app/services/user.service.ts index 1952179..7cfe567 100644 --- a/Client/src/app/services/user.service.ts +++ b/Client/src/app/services/user.service.ts @@ -57,4 +57,8 @@ export class UserService { deletePhoto(userId: number, photoId: number) { return this.http.delete(`${this.usersUrl}${userId}/photos/${photoId}`); } + + likeUser(userId: number, recipientId: number) { + return this.http.post(`${this.usersUrl}${userId}/like/${recipientId}`, {}); + } } diff --git a/Data/FriendRepository.cs b/Data/FriendRepository.cs index 2bf4963..7753581 100644 --- a/Data/FriendRepository.cs +++ b/Data/FriendRepository.cs @@ -2,6 +2,7 @@ using Friendster.Models; using Microsoft.EntityFrameworkCore; using System; +using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; @@ -58,6 +59,18 @@ public async Task> GetUsers(UserParameters parameters) u.BirthDate >= minimumBirthDate && u.BirthDate <= maximumBirthDate); + if (parameters.Likers) + { + var likers = await GetUserLikes(parameters.UserId, true); + users = users.Where(u => likers.Contains(u.Id)); + } + + if (parameters.Likees) + { + var likees = await GetUserLikes(parameters.UserId, false); + users = users.Where(u => likees.Contains(u.Id)); + } + string orderBy = parameters.OrderBy?.ToLower(); if (!string.IsNullOrWhiteSpace(orderBy)) { @@ -84,5 +97,25 @@ public async Task SaveChangesAsync() { return await _context.SaveChangesAsync() > 0; } + + private async Task> GetUserLikes(int userId, bool getLikers) + { + var user = await _context.Users + .Include(x => x.Likers) + .Include(x => x.Likees) + .FirstOrDefaultAsync(u => u.Id == userId); + + if (getLikers) + { + return user.Likers + .Where(u => u.LikeeId == userId) + .Select(u => u.LikerId); + + } + + return user.Likees + .Where(u => u.LikerId == userId) + .Select(u => u.LikeeId); + } } } diff --git a/Helpers/UserParameters.cs b/Helpers/UserParameters.cs index d7f155c..04926fc 100644 --- a/Helpers/UserParameters.cs +++ b/Helpers/UserParameters.cs @@ -27,5 +27,7 @@ public int PageSize public int MinimumAge { get; set; } = 18; public int MaximumAge { get; set; } = 99; public string OrderBy { get; set; } + public bool Likers { get; set; } + public bool Likees { get; set; } } }