From e96d3e24c26d1e6857d64ab7df882ed9b68e06c8 Mon Sep 17 00:00:00 2001 From: Task Grading Bot Date: Sun, 7 Sep 2025 02:17:17 +0300 Subject: [PATCH 1/4] Add: Adding Docs for APIs --- APIs/Auth.md | 149 ++++++++++++++++++ APIs/User.md | 105 ++++++++++++ AskFm/AskFm.API/Controllers/AuthController.cs | 2 +- AskFm/AskFm.API/Controllers/UserController.cs | 2 +- 4 files changed, 256 insertions(+), 2 deletions(-) create mode 100644 APIs/Auth.md create mode 100644 APIs/User.md diff --git a/APIs/Auth.md b/APIs/Auth.md new file mode 100644 index 0000000..1918d5a --- /dev/null +++ b/APIs/Auth.md @@ -0,0 +1,149 @@ +# Endpoints for Authentication + +### Register +POST: `/api/Auth/register` + +**Request** +```json +{ + "name": "string", + "username": "string", + "email": "string", + "bio": "string", + "avatarPath": "string", + "passwrod": "string", + "lastSeen": "2025-09-06T22:27:00.026Z" +} +``` +**Response** + +Status Code: 200 + +```json +{ + "success": true, + "errors": [ + "string" + ], + "data": { + "isAuthenticated": true, + "token": "string", + "refreshToken": { + "token": "string", + "expireOn": "2025-09-06T22:57:05.109Z", + "isExpired": true, + "revokedOn": "2025-09-06T22:57:05.109Z", + "isActive": true, + "createdOn": "2025-09-06T22:57:05.109Z" + }, + "user": { + "name": "string", + "email": "string", + "lastSeen": "2025-09-06T22:57:05.109Z", + "bio": "string", + "avatarPath": "string", + "followerCount": 0 + } + } +} +``` + +----- +### Login +POST: `/api/Auth/login` + +**Request** +```json +{ + "email": "string", + "password": "string" + +} +``` +**Response** + +Status Code: 200 + +```json +{ + "success": true, + "errors": [ + "string" + ], + "data": { + "isAuthenticated": true, + "token": "string", + "refreshToken": { + "token": "string", + "expireOn": "2025-09-06T22:57:05.109Z", + "isExpired": true, + "revokedOn": "2025-09-06T22:57:05.109Z", + "isActive": true, + "createdOn": "2025-09-06T22:57:05.109Z" + }, + "user": { + "name": "string", + "email": "string", + "lastSeen": "2025-09-06T22:57:05.109Z", + "bio": "string", + "avatarPath": "string", + "followerCount": 0 + } + } +} +``` + +------ +### Refresh Token +POST: `/api/Auth/refresh-token/{id}` +Description: refersh token when jwt token is expired + +**Response** + +Status Code: 200 +```json +{ + "success": true, + "errors": [ + "string" + ], + "data": { + "isAuthenticated": true, + "token": "string", + "refreshToken": { + "token": "string", + "expireOn": "2025-09-06T22:57:05.109Z", + "isExpired": true, + "revokedOn": "2025-09-06T22:57:05.109Z", + "isActive": true, + "createdOn": "2025-09-06T22:57:05.109Z" + }, + "user": { + "name": "string", + "email": "string", + "lastSeen": "2025-09-06T22:57:05.109Z", + "bio": "string", + "avatarPath": "string", + "followerCount": 0 + } + } +} +``` + +--------- +### Logout +POST: `/api/Auth/logout/{id}` +Description: refersh token when jwt token is expired + +**Response** + +Status Code: 200 +```json +{ + "success": true, + "errors": [ + "string" + ], + "data": true +} +``` diff --git a/APIs/User.md b/APIs/User.md new file mode 100644 index 0000000..8fb39fa --- /dev/null +++ b/APIs/User.md @@ -0,0 +1,105 @@ +# Endpoints for User +### Get current user Profile +GET: `/api/User/profile` + +**Response** + +Status Code: 200 + +```json +{ + "name": "string", + "email": "string", + "lastSeen": "2025-09-06T23:04:42.634Z", + "bio": "string", + "avatarPath": "string", + "followerCount": 0 +} +``` + +--------- +### Get User By +GET: `/api/User/profile/{userId}` +Description: Get user by id + +**Response** + +Status Code: 200 + +```json +{ + "name": "string", + "email": "string", + "lastSeen": "2025-09-06T23:04:42.634Z", + "bio": "string", + "avatarPath": "string", + "followerCount": 0 +} +``` + +--------- +### Delete current user +Delete: `/api/User/profile/{userId}` + +**Response** +Status Code: 200 + +--------- +### Update User +POST: `/api/User/profile/update/{userId}` + +**Request** +```json +{ + "name": "string", + "bio": "string", + "avatarPath": "string" +} +``` + +**Response** + +Status Code: 200 + +```json +{ + "name": "string", + "email": "string", + "lastSeen": "2025-09-06T23:10:21.411Z", + "bio": "string", + "avatarPath": "string", + "followerCount": 0 +} +``` + +--------- +### Follow +POST: `/api/User/profile/{followerId}/follow/{targetUserId}` + +**Response** + +Status Code: 200 + +--------- +### Unfollow +POST: `/api/User/profile/{followerId}/unfollow/{targetUserId}` + +**Response** + +Status Code: 200 + +--------- +### Update Password +GET: `/api/User/profile/update/pass/{userId}` + +**Request** +```json +{ + "currentPassword": "string", + "updatedPassword": "string" +} +``` + +**Response** + +Status Code: 200 diff --git a/AskFm/AskFm.API/Controllers/AuthController.cs b/AskFm/AskFm.API/Controllers/AuthController.cs index 355b213..9760609 100644 --- a/AskFm/AskFm.API/Controllers/AuthController.cs +++ b/AskFm/AskFm.API/Controllers/AuthController.cs @@ -56,7 +56,7 @@ public async Task Login(LoginDTO login) return Ok(result); } - [HttpGet] + [HttpPost] [Route("refresh-token/{id}")] [Authorize(AuthenticationSchemes = "Bearer")] public async Task RefreshToken(int id) diff --git a/AskFm/AskFm.API/Controllers/UserController.cs b/AskFm/AskFm.API/Controllers/UserController.cs index 3f60899..894daed 100644 --- a/AskFm/AskFm.API/Controllers/UserController.cs +++ b/AskFm/AskFm.API/Controllers/UserController.cs @@ -72,7 +72,7 @@ public async Task UpdateUserAsync(int userId, UpdateUserDTO updat } var userRead = await _userService.GetUserByIdAsync(userId); - return Ok(userRead); + return Ok(userRead.Data); } [HttpDelete] From 7a0c157515c1935ca697dc4c0593e36325409122 Mon Sep 17 00:00:00 2001 From: Task Grading Bot Date: Tue, 9 Sep 2025 04:56:33 +0300 Subject: [PATCH 2/4] Redis step (locally) --- AskFm/AskFm.API/AskFm.API.csproj | 2 + AskFm/AskFm.API/Program.cs | 10 +++++ AskFm/AskFm.API/appsettings.json | 3 +- AskFm/AskFm.BLL/Services/RedisCacheService.cs | 37 +++++++++++++++++++ 4 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 AskFm/AskFm.BLL/Services/RedisCacheService.cs diff --git a/AskFm/AskFm.API/AskFm.API.csproj b/AskFm/AskFm.API/AskFm.API.csproj index 77d8e4c..59c3c20 100644 --- a/AskFm/AskFm.API/AskFm.API.csproj +++ b/AskFm/AskFm.API/AskFm.API.csproj @@ -18,7 +18,9 @@ + + diff --git a/AskFm/AskFm.API/Program.cs b/AskFm/AskFm.API/Program.cs index c06620b..efe32e8 100644 --- a/AskFm/AskFm.API/Program.cs +++ b/AskFm/AskFm.API/Program.cs @@ -150,6 +150,16 @@ public static void Main(string[] args) }) .AddEntityFrameworkStores() .AddDefaultTokenProviders(); + + + builder.Services.AddStackExchangeRedisCache(options => + { + options.Configuration = builder.Configuration.GetConnectionString("Redis"); + options.InstanceName = "AskFmCache"; + }); + + builder.Services.AddSingleton(); + var app = builder.Build(); diff --git a/AskFm/AskFm.API/appsettings.json b/AskFm/AskFm.API/appsettings.json index 779f8b0..0ea25cc 100644 --- a/AskFm/AskFm.API/appsettings.json +++ b/AskFm/AskFm.API/appsettings.json @@ -1,6 +1,7 @@ { "ConnectionStrings": { - "DefaultConnection": "CONNECTIONSTRING" + "DefaultConnection": "CONNECTIONSTRING", + "Redis": "localhost:6379" }, "Logging": { "LogLevel": { diff --git a/AskFm/AskFm.BLL/Services/RedisCacheService.cs b/AskFm/AskFm.BLL/Services/RedisCacheService.cs new file mode 100644 index 0000000..03ae1be --- /dev/null +++ b/AskFm/AskFm.BLL/Services/RedisCacheService.cs @@ -0,0 +1,37 @@ +using Microsoft.Extensions.Caching.Distributed; +using System.Text.Json; + +namespace AskFm.BLL.Services; + +public class RedisCacheService +{ + private readonly IDistributedCache _cache; + + public RedisCacheService(IDistributedCache cache) + { + _cache = cache; + } + + public async Task SetCacheAsync(string key, T value, TimeSpan expirationTime) + { + var options = new DistributedCacheEntryOptions + { + AbsoluteExpirationRelativeToNow = expirationTime + }; + + var jsonData = JsonSerializer.Serialize(value); + await _cache.SetStringAsync(key, jsonData, options); + } + + public async Task GetCacheAsync(string key) + { + var jsonData = await _cache.GetStringAsync(key); + return jsonData is null ? default : JsonSerializer.Deserialize(jsonData); + } + + public async Task RemoveCacheAsync(string key) + { + await _cache.RemoveAsync(key); + } +} + From 4008027aa935c2ec2d3a0af974c6ae22110e1a3b Mon Sep 17 00:00:00 2001 From: Task Grading Bot Date: Tue, 9 Sep 2025 06:22:06 +0300 Subject: [PATCH 3/4] Revert "Add: Adding Docs for APIs" This reverts commit e96d3e24c26d1e6857d64ab7df882ed9b68e06c8. --- APIs/Auth.md | 149 ------------------ APIs/User.md | 105 ------------ AskFm/AskFm.API/Controllers/AuthController.cs | 2 +- AskFm/AskFm.API/Controllers/UserController.cs | 2 +- 4 files changed, 2 insertions(+), 256 deletions(-) delete mode 100644 APIs/Auth.md delete mode 100644 APIs/User.md diff --git a/APIs/Auth.md b/APIs/Auth.md deleted file mode 100644 index 1918d5a..0000000 --- a/APIs/Auth.md +++ /dev/null @@ -1,149 +0,0 @@ -# Endpoints for Authentication - -### Register -POST: `/api/Auth/register` - -**Request** -```json -{ - "name": "string", - "username": "string", - "email": "string", - "bio": "string", - "avatarPath": "string", - "passwrod": "string", - "lastSeen": "2025-09-06T22:27:00.026Z" -} -``` -**Response** - -Status Code: 200 - -```json -{ - "success": true, - "errors": [ - "string" - ], - "data": { - "isAuthenticated": true, - "token": "string", - "refreshToken": { - "token": "string", - "expireOn": "2025-09-06T22:57:05.109Z", - "isExpired": true, - "revokedOn": "2025-09-06T22:57:05.109Z", - "isActive": true, - "createdOn": "2025-09-06T22:57:05.109Z" - }, - "user": { - "name": "string", - "email": "string", - "lastSeen": "2025-09-06T22:57:05.109Z", - "bio": "string", - "avatarPath": "string", - "followerCount": 0 - } - } -} -``` - ------ -### Login -POST: `/api/Auth/login` - -**Request** -```json -{ - "email": "string", - "password": "string" - -} -``` -**Response** - -Status Code: 200 - -```json -{ - "success": true, - "errors": [ - "string" - ], - "data": { - "isAuthenticated": true, - "token": "string", - "refreshToken": { - "token": "string", - "expireOn": "2025-09-06T22:57:05.109Z", - "isExpired": true, - "revokedOn": "2025-09-06T22:57:05.109Z", - "isActive": true, - "createdOn": "2025-09-06T22:57:05.109Z" - }, - "user": { - "name": "string", - "email": "string", - "lastSeen": "2025-09-06T22:57:05.109Z", - "bio": "string", - "avatarPath": "string", - "followerCount": 0 - } - } -} -``` - ------- -### Refresh Token -POST: `/api/Auth/refresh-token/{id}` -Description: refersh token when jwt token is expired - -**Response** - -Status Code: 200 -```json -{ - "success": true, - "errors": [ - "string" - ], - "data": { - "isAuthenticated": true, - "token": "string", - "refreshToken": { - "token": "string", - "expireOn": "2025-09-06T22:57:05.109Z", - "isExpired": true, - "revokedOn": "2025-09-06T22:57:05.109Z", - "isActive": true, - "createdOn": "2025-09-06T22:57:05.109Z" - }, - "user": { - "name": "string", - "email": "string", - "lastSeen": "2025-09-06T22:57:05.109Z", - "bio": "string", - "avatarPath": "string", - "followerCount": 0 - } - } -} -``` - ---------- -### Logout -POST: `/api/Auth/logout/{id}` -Description: refersh token when jwt token is expired - -**Response** - -Status Code: 200 -```json -{ - "success": true, - "errors": [ - "string" - ], - "data": true -} -``` diff --git a/APIs/User.md b/APIs/User.md deleted file mode 100644 index 8fb39fa..0000000 --- a/APIs/User.md +++ /dev/null @@ -1,105 +0,0 @@ -# Endpoints for User -### Get current user Profile -GET: `/api/User/profile` - -**Response** - -Status Code: 200 - -```json -{ - "name": "string", - "email": "string", - "lastSeen": "2025-09-06T23:04:42.634Z", - "bio": "string", - "avatarPath": "string", - "followerCount": 0 -} -``` - ---------- -### Get User By -GET: `/api/User/profile/{userId}` -Description: Get user by id - -**Response** - -Status Code: 200 - -```json -{ - "name": "string", - "email": "string", - "lastSeen": "2025-09-06T23:04:42.634Z", - "bio": "string", - "avatarPath": "string", - "followerCount": 0 -} -``` - ---------- -### Delete current user -Delete: `/api/User/profile/{userId}` - -**Response** -Status Code: 200 - ---------- -### Update User -POST: `/api/User/profile/update/{userId}` - -**Request** -```json -{ - "name": "string", - "bio": "string", - "avatarPath": "string" -} -``` - -**Response** - -Status Code: 200 - -```json -{ - "name": "string", - "email": "string", - "lastSeen": "2025-09-06T23:10:21.411Z", - "bio": "string", - "avatarPath": "string", - "followerCount": 0 -} -``` - ---------- -### Follow -POST: `/api/User/profile/{followerId}/follow/{targetUserId}` - -**Response** - -Status Code: 200 - ---------- -### Unfollow -POST: `/api/User/profile/{followerId}/unfollow/{targetUserId}` - -**Response** - -Status Code: 200 - ---------- -### Update Password -GET: `/api/User/profile/update/pass/{userId}` - -**Request** -```json -{ - "currentPassword": "string", - "updatedPassword": "string" -} -``` - -**Response** - -Status Code: 200 diff --git a/AskFm/AskFm.API/Controllers/AuthController.cs b/AskFm/AskFm.API/Controllers/AuthController.cs index 9760609..355b213 100644 --- a/AskFm/AskFm.API/Controllers/AuthController.cs +++ b/AskFm/AskFm.API/Controllers/AuthController.cs @@ -56,7 +56,7 @@ public async Task Login(LoginDTO login) return Ok(result); } - [HttpPost] + [HttpGet] [Route("refresh-token/{id}")] [Authorize(AuthenticationSchemes = "Bearer")] public async Task RefreshToken(int id) diff --git a/AskFm/AskFm.API/Controllers/UserController.cs b/AskFm/AskFm.API/Controllers/UserController.cs index 894daed..3f60899 100644 --- a/AskFm/AskFm.API/Controllers/UserController.cs +++ b/AskFm/AskFm.API/Controllers/UserController.cs @@ -72,7 +72,7 @@ public async Task UpdateUserAsync(int userId, UpdateUserDTO updat } var userRead = await _userService.GetUserByIdAsync(userId); - return Ok(userRead.Data); + return Ok(userRead); } [HttpDelete] From 8e022aaf82238845c97a7e93d6eab902b18e6425 Mon Sep 17 00:00:00 2001 From: Task Grading Bot Date: Tue, 9 Sep 2025 07:48:23 +0300 Subject: [PATCH 4/4] reids service interface --- AskFm/AskFm.BLL/Services/IRedisService.cs | 14 ++++++++++++++ AskFm/AskFm.BLL/Services/RedisCacheService.cs | 2 +- 2 files changed, 15 insertions(+), 1 deletion(-) create mode 100644 AskFm/AskFm.BLL/Services/IRedisService.cs diff --git a/AskFm/AskFm.BLL/Services/IRedisService.cs b/AskFm/AskFm.BLL/Services/IRedisService.cs new file mode 100644 index 0000000..ae55879 --- /dev/null +++ b/AskFm/AskFm.BLL/Services/IRedisService.cs @@ -0,0 +1,14 @@ +using Microsoft.Extensions.Caching.Distributed; +using System.Text.Json; + +namespace AskFm.BLL.Services; + +public interface IRedisService +{ + public Task SetCacheAsync(string key, T value, TimeSpan expirationTime); + + public Task GetCacheAsync(string key); + + public Task RemoveCacheAsync(string key); +} + diff --git a/AskFm/AskFm.BLL/Services/RedisCacheService.cs b/AskFm/AskFm.BLL/Services/RedisCacheService.cs index 03ae1be..0c95a83 100644 --- a/AskFm/AskFm.BLL/Services/RedisCacheService.cs +++ b/AskFm/AskFm.BLL/Services/RedisCacheService.cs @@ -3,7 +3,7 @@ namespace AskFm.BLL.Services; -public class RedisCacheService +public class RedisCacheService : IRedisService { private readonly IDistributedCache _cache;