Skip to content

Commit

Permalink
Wire up like functionality on front-end
Browse files Browse the repository at this point in the history
  • Loading branch information
marvac committed Feb 22, 2019
1 parent f6b5fb7 commit 376322d
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<button class="btn btn-primary" [routerLink]="['/members/', user.id]"><i class="fa fa-user"></i> </button>
</li>
<li class="list-inline-item">
<button class="btn btn-danger"><i class="fa fa-heart"></i> </button>
<button class="btn btn-danger" (click)="likeUser(user.id)"><i class="fa fa-heart"></i> </button>
</li>
<li class="list-inline-item">
<button class="btn btn-warning"><i class="fa fa-envelope"></i> </button>
Expand Down
Original file line number Diff line number Diff line change
@@ -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',
Expand All @@ -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);
});
}

}
4 changes: 4 additions & 0 deletions Client/src/app/services/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`, {});
}
}
33 changes: 33 additions & 0 deletions Data/FriendRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Friendster.Models;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

Expand Down Expand Up @@ -58,6 +59,18 @@ public async Task<PagedList<User>> 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))
{
Expand All @@ -84,5 +97,25 @@ public async Task<bool> SaveChangesAsync()
{
return await _context.SaveChangesAsync() > 0;
}

private async Task<IEnumerable<int>> 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);
}
}
}
2 changes: 2 additions & 0 deletions Helpers/UserParameters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
}
}

0 comments on commit 376322d

Please sign in to comment.