-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
191 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
using Friendster.Models; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
|
||
namespace Friendster.Controllers.Resources | ||
{ | ||
public class DetailUserResource | ||
{ | ||
public int Id { get; set; } | ||
public string Username { get; set; } | ||
public Gender Gender { get; set; } | ||
public int Age { get; set; } | ||
public string KnownAs { get; set; } | ||
public DateTime DateCreated { get; set; } | ||
public DateTime LastActive { get; set; } | ||
public string Introduction { get; set; } | ||
public Gender LookingFor { get; set; } | ||
public string Interests { get; set; } | ||
public string City { get; set; } | ||
public string Country { get; set; } | ||
public string PhotoUrl { get; set; } | ||
public ICollection<Photo> Photos { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
using Friendster.Models; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
|
||
namespace Friendster.Controllers.Resources | ||
{ | ||
public class ListUserResource | ||
{ | ||
public int Id { get; set; } | ||
public string Username { get; set; } | ||
public Gender Gender { get; set; } | ||
public int Age { get; set; } | ||
public string KnownAs { get; set; } | ||
public DateTime DateCreated { get; set; } | ||
public DateTime LastActive { get; set; } | ||
public string Introduction { get; set; } | ||
public Gender LookingFor { get; set; } | ||
public string Interests { get; set; } | ||
public string City { get; set; } | ||
public string Country { get; set; } | ||
public string PhotoUrl { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
using System; | ||
using System.IdentityModel.Tokens.Jwt; | ||
using System.Security.Claims; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using Friendster.Controllers.Resources; | ||
using Friendster.Data; | ||
using Friendster.Models; | ||
using Microsoft.AspNetCore.Authorization; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.IdentityModel.Tokens; | ||
|
||
namespace Friendster.Controllers | ||
{ | ||
[Route("api/[controller]")] | ||
[Authorize] | ||
[ApiController] | ||
public class UsersController : ControllerBase | ||
{ | ||
private IFriendRepository _repo; | ||
|
||
public UsersController(IFriendRepository repo) | ||
{ | ||
_repo = repo; | ||
} | ||
|
||
[HttpGet] | ||
public async Task<IActionResult> GetUsers() | ||
{ | ||
var users = await _repo.GetUsers(); | ||
return Ok(users); | ||
} | ||
|
||
[HttpGet("{userId}")] | ||
public async Task<IActionResult> GetUser(int userId) | ||
{ | ||
var user = await _repo.GetUser(userId); | ||
return Ok(user); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Friendster.Models; | ||
using Microsoft.EntityFrameworkCore; | ||
|
||
namespace Friendster.Data | ||
{ | ||
public class FriendRepository : IFriendRepository | ||
{ | ||
private DataContext _context; | ||
|
||
public FriendRepository(DataContext context) | ||
{ | ||
_context = context; | ||
} | ||
|
||
public void Add<T>(T entity) where T : class | ||
{ | ||
_context.Add(entity); | ||
} | ||
|
||
public void Delete<T>(T entity) where T : class | ||
{ | ||
_context.Remove(entity); | ||
} | ||
|
||
public async Task<User> GetUser(int userId) | ||
{ | ||
var user = await _context.Users | ||
.Include(p => p.Photos) | ||
.FirstOrDefaultAsync(x => x.Id == userId); | ||
|
||
return user; | ||
} | ||
|
||
public async Task<IEnumerable<User>> GetUsers() | ||
{ | ||
var users = await _context.Users | ||
.Include(p => p.Photos) | ||
.ToListAsync(); | ||
|
||
return users; | ||
} | ||
|
||
public async Task<bool> SaveChangesAsync() | ||
{ | ||
return await _context.SaveChangesAsync() > 0; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
using Friendster.Models; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
|
||
namespace Friendster.Data | ||
{ | ||
public interface IFriendRepository | ||
{ | ||
void Add<T>(T entity) where T : class; | ||
void Delete<T>(T entity) where T : class; | ||
Task<bool> SaveChangesAsync(); | ||
Task<IEnumerable<User>> GetUsers(); | ||
Task<User> GetUser(int userId); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters