Skip to content

Commit

Permalink
Add member repository and models
Browse files Browse the repository at this point in the history
  • Loading branch information
marvac committed Jan 14, 2019
1 parent b5b66bc commit 7a7b168
Show file tree
Hide file tree
Showing 7 changed files with 191 additions and 21 deletions.
26 changes: 26 additions & 0 deletions Controllers/Resources/DetailUserResource.cs
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; }
}
}
25 changes: 25 additions & 0 deletions Controllers/Resources/ListUserResource.cs
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; }
}
}
42 changes: 42 additions & 0 deletions Controllers/UsersController.cs
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);
}
}
}
52 changes: 52 additions & 0 deletions Data/FriendRepository.cs
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;
}
}
}
17 changes: 17 additions & 0 deletions Data/IFriendRepository.cs
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);
}
}
40 changes: 20 additions & 20 deletions Data/Seeds/UserSeeds.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
{
"Username": "Bridgette",
"Gender": 1,
"DateOfBirth": "1986-08-06",
"BirthDate": "1986-08-06",
"Password": "password",
"KnownAs": "Bridgette",
"Created": "2017-08-08",
"DateCreated": "2017-08-08",
"LastActive": "2017-08-08",
"Introduction": "Laboris cillum enim velit ad incididunt in ullamco eiusmod sunt deserunt nulla fugiat consectetur voluptate. Occaecat non et in ipsum deserunt in pariatur eu reprehenderit incididunt incididunt laborum qui duis. Enim pariatur anim incididunt anim quis nisi adipisicing cillum sit. Commodo irure est nulla enim dolor dolor eiusmod cillum eu id. Excepteur ipsum mollit fugiat do cillum ea duis cupidatat aliquip aliqua culpa. Culpa velit incididunt ex adipisicing irure ipsum.\r\n",
"LookingFor": 2,
Expand All @@ -23,10 +23,10 @@
{
"Username": "Karen",
"Gender": 1,
"DateOfBirth": "1979-06-04",
"BirthDate": "1979-06-04",
"Password": "password",
"KnownAs": "Karen",
"Created": "2017-02-26",
"DateCreated": "2017-02-26",
"LastActive": "2017-02-26",
"Introduction": "Cillum do excepteur sint in Lorem magna laboris fugiat ad laborum. Ex deserunt quis nulla nostrud. Exercitation velit aute non est sit voluptate. Est nostrud laboris duis mollit ea fugiat laboris velit cillum. Cillum nostrud eu excepteur cupidatat veniam dolor laboris magna consectetur quis. Anim nisi anim tempor deserunt deserunt consequat ex proident non.\r\n",
"LookingFor": 0,
Expand All @@ -44,10 +44,10 @@
{
"Username": "Patricia",
"Gender": 1,
"DateOfBirth": "1989-05-07",
"BirthDate": "1989-05-07",
"Password": "password",
"KnownAs": "Patricia",
"Created": "2017-04-18",
"DateCreated": "2017-04-18",
"LastActive": "2017-04-18",
"Introduction": "Nisi ullamco eu excepteur id reprehenderit labore incididunt. Do laborum irure incididunt ullamco ut deserunt elit dolor minim et anim consequat cupidatat. Quis exercitation irure reprehenderit fugiat aute do adipisicing dolor culpa sunt aliqua est et cupidatat. Eu elit aliqua quis in minim magna amet. Anim eu ea commodo mollit nostrud qui deserunt reprehenderit.\r\n",
"LookingFor": 1,
Expand All @@ -65,10 +65,10 @@
{
"Username": "Jill",
"Gender": 1,
"DateOfBirth": "1989-01-25",
"BirthDate": "1989-01-25",
"Password": "password",
"KnownAs": "Jill",
"Created": "2017-04-14",
"DateCreated": "2017-04-14",
"LastActive": "2017-04-14",
"Introduction": "Ad cillum occaecat esse laboris minim incididunt reprehenderit esse Lorem irure. Consequat laboris officia occaecat exercitation aliqua dolor ullamco occaecat. Fugiat duis sit sint et adipisicing cillum nostrud sit irure anim nostrud elit.\r\n",
"LookingFor": 1,
Expand All @@ -86,10 +86,10 @@
{
"Username": "Rosemarie",
"Gender": 1,
"DateOfBirth": "1971-06-26",
"BirthDate": "1971-06-26",
"Password": "password",
"KnownAs": "Rosemarie",
"Created": "2017-01-10",
"DateCreated": "2017-01-10",
"LastActive": "2017-01-10",
"Introduction": "Consequat commodo consectetur minim anim cillum mollit eiusmod tempor. Consequat magna in proident exercitation cupidatat adipisicing duis. Cillum incididunt enim ea tempor ipsum in commodo ex.\r\n",
"LookingFor": 0,
Expand All @@ -107,10 +107,10 @@
{
"Username": "Kate",
"Gender": 1,
"DateOfBirth": "1982-06-06",
"BirthDate": "1982-06-06",
"Password": "password",
"KnownAs": "Kate",
"Created": "2017-04-08",
"DateCreated": "2017-04-08",
"LastActive": "2017-04-08",
"Introduction": "Qui elit nisi in aliquip Lorem minim qui deserunt exercitation minim laboris elit ex. Laboris officia laborum anim mollit aliquip sit cupidatat esse dolor non dolore. Adipisicing quis est ex nostrud laboris eu consectetur quis. Mollit esse sit nisi duis aliquip eu esse exercitation Lorem duis. Ut veniam aliqua id eu.\r\n",
"LookingFor": 0,
Expand All @@ -128,10 +128,10 @@
{
"Username": "Molly",
"Gender": 1,
"DateOfBirth": "1976-09-15",
"BirthDate": "1976-09-15",
"Password": "password",
"KnownAs": "Molly",
"Created": "2017-06-05",
"DateCreated": "2017-06-05",
"LastActive": "2017-06-05",
"Introduction": "Aute cillum adipisicing irure non consequat labore. Reprehenderit fugiat eiusmod esse sit. Ullamco irure dolor consequat elit eiusmod pariatur. Officia voluptate commodo incididunt ullamco.\r\n",
"LookingFor": 0,
Expand All @@ -149,10 +149,10 @@
{
"Username": "Amy",
"Gender": 1,
"DateOfBirth": "1996-08-08",
"BirthDate": "1996-08-08",
"Password": "password",
"KnownAs": "Amy",
"Created": "2017-04-11",
"DateCreated": "2017-04-11",
"LastActive": "2017-04-11",
"Introduction": "Incididunt quis occaecat deserunt magna et. Laborum eu veniam cupidatat magna id excepteur. Cupidatat exercitation officia dolore tempor pariatur. Dolor occaecat ipsum quis quis excepteur qui quis aliquip deserunt exercitation cupidatat ea ex commodo. Elit culpa duis pariatur culpa aliquip. Pariatur laboris duis ad occaecat voluptate aliquip enim dolore ea dolore. Laborum elit sit nulla ut pariatur occaecat.\r\n",
"LookingFor": 0,
Expand All @@ -170,10 +170,10 @@
{
"Username": "Tereso",
"Gender": 0,
"DateOfBirth": "1951-05-26",
"BirthDate": "1951-05-26",
"Password": "password",
"KnownAs": "Tereso",
"Created": "2017-05-08",
"DateCreated": "2017-05-08",
"LastActive": "2017-05-08",
"Introduction": "Proident tempor ut consectetur cupidatat incididunt consectetur velit dolor enim sint exercitation nisi ex enim. Minim quis cupidatat minim et elit magna duis ea labore anim eu dolore consequat. Voluptate aute quis dolor incididunt duis consectetur magna pariatur eiusmod. Ea cillum id mollit est amet consectetur nisi do. Exercitation eiusmod fugiat elit deserunt. Nisi dolore cillum consequat aliquip voluptate esse eu.\r\n",
"LookingFor": 1,
Expand All @@ -191,10 +191,10 @@
{
"Username": "Cryder",
"Gender": 0,
"DateOfBirth": "1968-01-14",
"BirthDate": "1968-01-14",
"Password": "password",
"KnownAs": "Cryder",
"Created": "2017-08-13",
"DateCreated": "2017-08-13",
"LastActive": "2017-08-13",
"Introduction": "Dolor minim exercitation velit minim. Eu mollit nisi aliqua cillum laboris ipsum ad magna sit qui aliqua minim est occaecat. Minim duis reprehenderit aliqua quis cillum nisi duis culpa sunt. Amet tempor tempor eu esse cupidatat aute elit aute. Deserunt cupidatat sit elit et ullamco. Incididunt velit ipsum dolore cillum.\r\n",
"LookingFor": 1,
Expand Down
10 changes: 9 additions & 1 deletion Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using Microsoft.Extensions.DependencyInjection;
using Friendster.Helpers;
using Microsoft.IdentityModel.Tokens;
using Newtonsoft.Json;

namespace Friendster
{
Expand All @@ -33,8 +34,11 @@ public void ConfigureServices(IServiceCollection services)
});

services.AddTransient<Seeder>();

services.AddScoped<IDataRepository, DataRepository>();
services.AddScoped<IAuthRepository, AuthRepository>();
services.AddScoped<IFriendRepository, FriendRepository>();

services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(options =>
{
Expand All @@ -47,7 +51,11 @@ public void ConfigureServices(IServiceCollection services)
};
});

services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1)
.AddJsonOptions(options =>
{
options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
});

services.AddCors();
}
Expand Down

0 comments on commit 7a7b168

Please sign in to comment.