Skip to content

Commit 231740a

Browse files
authored
Merge pull request #10 from nano-devs/development
Development v0.1.0
2 parents 1b7c201 + 9e2af46 commit 231740a

File tree

116 files changed

+2906
-2564
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

116 files changed

+2906
-2564
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -454,3 +454,5 @@ $RECYCLE.BIN/
454454
!.vscode/extensions.json
455455

456456
*.db
457+
*.pem
458+
*-key.pem
Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
<Project Sdk="Microsoft.NET.Sdk.Web">
22

33
<PropertyGroup>
4-
<TargetFramework>net5.0</TargetFramework>
5-
<StartupObject>NET5ChatAppServerAPI.Program</StartupObject>
6-
<SatelliteResourceLanguages>en</SatelliteResourceLanguages>
4+
<TargetFramework>net6.0</TargetFramework>
5+
<Nullable>enable</Nullable>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<NeutralLanguage>en-001</NeutralLanguage>
78
</PropertyGroup>
89

910
<ItemGroup>
10-
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="5.0.11" />
11-
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="5.0.9" />
12-
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="5.0.11">
11+
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="6.0.1" />
12+
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.1" />
13+
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="6.0.1" />
14+
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="6.0.1">
1315
<PrivateAssets>all</PrivateAssets>
1416
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
1517
</PackageReference>
16-
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="5.0.7" />
17-
<PackageReference Include="Swashbuckle.AspNetCore" Version="5.6.3" />
18-
<PackageReference Include="System.Text.Json" Version="5.0.2" />
18+
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
19+
<PackageReference Include="System.Text.Json" Version="6.0.1" />
1920
</ItemGroup>
20-
2121
</Project>
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
namespace ChatApp.Api.Controllers;
2+
using ChatApp.Api.Services.Repositories;
3+
4+
using Microsoft.AspNetCore.Mvc;
5+
6+
[ApiController]
7+
[Route("[controller]/[action]")]
8+
public class FriendController : ControllerBase
9+
{
10+
protected IFriendsRepository _friendsRepository;
11+
12+
public FriendController(IFriendsRepository friendsRepository)
13+
{
14+
this._friendsRepository = friendsRepository;
15+
}
16+
17+
/// <summary>
18+
/// Get all friends.
19+
/// </summary>
20+
/// <returns></returns>
21+
[HttpGet]
22+
public async Task<object> Index()
23+
{
24+
var friends = await this._friendsRepository.GetAllAsync();
25+
26+
if (friends.Any())
27+
{
28+
return friends;
29+
}
30+
31+
return "no friends exist in database";
32+
}
33+
34+
/// <summary>
35+
/// Get the user all friends
36+
/// </summary>
37+
/// <param name="userId"></param>
38+
/// <returns></returns>
39+
[HttpGet]
40+
public async Task<object> MyFriends(Guid userId)
41+
{
42+
if (this._friendsRepository is null)
43+
{
44+
return "Friends context is null";
45+
}
46+
47+
var friends = await this._friendsRepository.GetFriendsAsync(userId);
48+
49+
if (friends.Any())
50+
{
51+
return friends;
52+
}
53+
else
54+
{
55+
return $"You ({ userId }) do not have a friend";
56+
}
57+
}
58+
59+
/// <summary>
60+
/// Add new friends
61+
/// </summary>
62+
/// <param name="groupId"></param>
63+
/// <param name="userpId"></param>
64+
/// <returns></returns>
65+
[HttpPost]
66+
public async Task<object> AddFriend(Guid userId, Guid friendId)
67+
{
68+
if (this._friendsRepository is null)
69+
{
70+
return "Friends context is null";
71+
}
72+
73+
if (await this._friendsRepository.IsFriendExistAsync(userId, friendId))
74+
{
75+
return $"{ userId } already a friend { friendId }";
76+
}
77+
78+
try
79+
{
80+
var success = await this._friendsRepository.AddFriendshipAsync(userId, friendId);
81+
if (success)
82+
{
83+
await this._friendsRepository.SaveAsync();
84+
return this.Ok();
85+
}
86+
87+
return "Failed to add { friendId } for { userId }";
88+
}
89+
catch
90+
{
91+
return "Failed to add { friendId } for { userId }";
92+
}
93+
}
94+
95+
/// <summary>
96+
/// Remove friend
97+
/// </summary>
98+
/// <param name="groupId"></param>
99+
/// <param name="userpId"></param>
100+
/// <returns></returns>
101+
[HttpPost]
102+
public async Task<object> RemoveFriend(Guid userId, Guid friendId)
103+
{
104+
if (this._friendsRepository is null)
105+
{
106+
return "Friends context is null";
107+
}
108+
109+
if (!await this._friendsRepository.IsFriendExistAsync(userId, friendId))
110+
{
111+
return $"{ userId } do not have a friend { friendId }";
112+
113+
}
114+
115+
try
116+
{
117+
var success = await this._friendsRepository.RemoveFriendshipAsync(userId, friendId);
118+
if (success)
119+
{
120+
await this._friendsRepository.SaveAsync();
121+
return this.Ok();
122+
}
123+
124+
return $"Failed to remove friend ({ friendId }) from user ({ userId })";
125+
}
126+
catch
127+
{
128+
return $"Failed to remove friend ({ friendId }) from user ({ userId })";
129+
}
130+
}
131+
}

0 commit comments

Comments
 (0)