Skip to content

Commit

Permalink
Merge pull request #169 from SCCapstone/socialSpa
Browse files Browse the repository at this point in the history
Social spa
  • Loading branch information
epadams authored Dec 6, 2023
2 parents 6aec213 + 6be8d84 commit 52c2196
Show file tree
Hide file tree
Showing 31 changed files with 654 additions and 35 deletions.
10 changes: 9 additions & 1 deletion FU.API/FU.API/Controllers/PostsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,15 @@ public async Task<IActionResult> GetPost(int postId)
return NotFound();
}

var response = post.ToDto();
var hasJoinedPost = false;
var user = await _postService.GetCurrentUser(User);

if (user is not null)
{
hasJoinedPost = await _postService.HasJoinedPost(user.UserId, postId);
}

var response = post.ToDto(hasJoined: hasJoinedPost);

return Ok(response);
}
Expand Down
29 changes: 25 additions & 4 deletions FU.API/FU.API/Controllers/SearchController.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
namespace FU.API.Controllers;

using FU.API.DTOs.Post;
using FU.API.DTOs.Search;
using FU.API.Exceptions;
using FU.API.Helpers;
using FU.API.Services;
using FU.API.Interfaces;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

[ApiController]
[Route("api/[controller]")]
public class SearchController : ControllerBase
{
private readonly SearchService _searchService;
private readonly ISearchService _searchService;

public SearchController(SearchService searchService)
public SearchController(ISearchService searchService)
{
_searchService = searchService;
}
Expand All @@ -21,6 +24,24 @@ public SearchController(SearchService searchService)
public async Task<IActionResult> SearchPosts([FromQuery] PostSearchRequestDTO request)
{
var posts = await _searchService.SearchPosts(request.ToPostQuery());
return Ok(posts.ToDtos());
var response = new List<PostResponseDTO>(posts.Count());

// Go through each post and check if the user has joined the post
var user = await _searchService.GetCurrentUser(User);

if (user is not null)
{
foreach (var post in posts)
{
var joined = await _searchService.HasJoinedPost(user.UserId, post.Id);
response.Add(post.ToDto(hasJoined: joined));
}
}
else
{
response = posts.ToDtos().ToList();
}

return Ok(response);
}
}
9 changes: 8 additions & 1 deletion FU.API/FU.API/Controllers/UsersController.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
namespace FU.API.Controllers;

using FU.API.DTOs.Post;
using FU.API.Exceptions;
using FU.API.Helpers;
using FU.API.Interfaces;
Expand Down Expand Up @@ -79,7 +80,13 @@ public async Task<IActionResult> GetUsersAssociatedPosts([FromQuery] int limit =

var posts = await _userService.GetUsersAssociatedPosts(user.UserId, limit, offset);

var response = posts.ToDtos();
var response = new List<PostResponseDTO>(posts.Count());

// for each resonse set has joined to true
foreach (var post in posts)
{
response.Add(post.ToDto(hasJoined: true));
}

return Ok(response);
}
Expand Down
2 changes: 2 additions & 0 deletions FU.API/FU.API/DTOs/Post/PostResponseDTO.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,6 @@ public class PostResponseDTO
public string Creator { get; set; } = string.Empty;

public ICollection<string> Tags { get; set; } = new HashSet<string>();

public bool HasJoined { get; set; } = false;
}
3 changes: 2 additions & 1 deletion FU.API/FU.API/Helpers/Mapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ public static PostQuery ToPostQuery(this PostSearchRequestDTO dto)
return query;
}

public static PostResponseDTO ToDto(this Post post)
public static PostResponseDTO ToDto(this Post post, bool hasJoined = false)
{
return new PostResponseDTO()
{
Expand All @@ -160,6 +160,7 @@ public static PostResponseDTO ToDto(this Post post)
ChatId = post.ChatId,
Creator = post.Creator.Username,
Tags = post.Tags.Select(t => t.Tag.Name).ToList(),
HasJoined = hasJoined,
};
}

Expand Down
14 changes: 10 additions & 4 deletions FU.API/FU.API/Hubs/ChatHub.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

using System;
using FU.API.Data;
using FU.API.Helpers;
using FU.API.Interfaces;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.SignalR;

Expand Down Expand Up @@ -36,7 +38,8 @@ public ChatHub(AppDbContext context)
/// <returns>Task.</returns>
public override Task OnConnectedAsync()
{
var user = _context.Users.FirstOrDefault(u => u.Username == IdentityName);
var userId = UserId;
var user = _context.Users.Find(userId);

if (user is null || user.Username is null)
{
Expand All @@ -63,7 +66,8 @@ public override Task OnConnectedAsync()
/// <returns>Task.</returns>
public override Task OnDisconnectedAsync(Exception? exception)
{
var user = _context.Users.FirstOrDefault(u => u.Username == IdentityName);
var userId = UserId;
var user = _context.Users.Find(userId);

if (user is null || user.Username is null)
{
Expand Down Expand Up @@ -104,7 +108,9 @@ public async Task LeaveChatGroup(int chatId)
}

/// <summary>
/// Gets the identity name from the context.
/// Gets the user id from the context.
/// </summary>
private string? IdentityName => Context?.User?.Identity?.Name;
private int UserId => Context?.User?.Claims?.FirstOrDefault(c => c.Type == CustomClaimTypes.UserId)?.Value is not null
? int.Parse(Context.User.Claims.FirstOrDefault(c => c.Type == CustomClaimTypes.UserId)?.Value ?? string.Empty)
: -1;
}
2 changes: 2 additions & 0 deletions FU.API/FU.API/Interfaces/ICommonService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ public interface ICommonService
Task<ApplicationUser?> GetCurrentUser(ClaimsPrincipal claims);

Task<ApplicationUser?> GetUser(int userId);

Task<bool> HasJoinedPost(int userId, int postId);
}
8 changes: 8 additions & 0 deletions FU.API/FU.API/Interfaces/ISearchService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace FU.API.Interfaces;

using FU.API.Models;

public interface ISearchService : ICommonService
{
Task<List<Post>> SearchPosts(PostQuery query);
}
11 changes: 9 additions & 2 deletions FU.API/FU.API/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
builder.Services.AddScoped<IChatService, ChatService>();
builder.Services.AddScoped<IGameService, GameService>();
builder.Services.AddScoped<ITagService, TagService>();
builder.Services.AddScoped<SearchService>();
builder.Services.AddScoped<ISearchService, SearchService>();
builder.Services.AddScoped<ICommonService, CommonService>();

// Add SignalR
Expand Down Expand Up @@ -144,7 +144,14 @@
}

// Allow any cors
app.UseCors(options => options.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader());
app.UseCors(x => x
.WithOrigins("http://localhost:5173")
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials());

app.UseAuthentication();
app.UseAuthorization();

app.UseHttpsRedirection();
app.UseExceptionHandler(new ExceptionHandlerOptions { ExceptionHandler = ExceptionHandler.HandleException });
Expand Down
1 change: 1 addition & 0 deletions FU.API/FU.API/Services/ChatService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ public async Task<Chat> CreateChat(ApplicationUser user, ChatType chatType, stri
.OrderByDescending(m => m.CreatedAt)
.Skip((offset - 1) * limit)
.Take(limit)
.Reverse()
.ToListAsync();
return messages;
}
Expand Down
17 changes: 16 additions & 1 deletion FU.API/FU.API/Services/CommonService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using FU.API.Helpers;
using FU.API.Interfaces;
using FU.API.Models;
using Microsoft.EntityFrameworkCore;
using System.Security.Claims;

public class CommonService : ICommonService
Expand All @@ -22,7 +23,7 @@ public CommonService(AppDbContext dbContext)

if (stringId is null || !int.TryParse(stringId, out int userId))
{
throw new UnauthorizedException();
return null;
}

// Get the user from the database
Expand All @@ -33,4 +34,18 @@ public CommonService(AppDbContext dbContext)
{
return await _dbContext.Users.FindAsync(userId);
}

public async Task<bool> HasJoinedPost(int userId, int postId)
{
var chat = await _dbContext.Posts
.Include(p => p.Chat)
.ThenInclude(c => c.Members)
.ThenInclude(cu => cu.User)
.Where(p => p.Id == postId)
.Select(p => p.Chat)
.FirstOrDefaultAsync();

var res = chat is not null && chat.Members.Any(m => m.UserId == userId);
return res;
}
}
1 change: 1 addition & 0 deletions FU.API/FU.API/Services/PostService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ public async Task JoinPost(int postId, ApplicationUser user)
var post = await _dbContext.Posts
.Where(p => p.Id == postId)
.Include(p => p.Chat)
.ThenInclude(c => c.Members)
.FirstOrDefaultAsync() ?? throw new PostException("Post does not exist");
var chat = post.Chat;
var userId = user.UserId;
Expand Down
4 changes: 3 additions & 1 deletion FU.API/FU.API/Services/SearchService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@ namespace FU.API.Services;

using System.Linq.Expressions;
using FU.API.Data;
using FU.API.Interfaces;
using FU.API.Models;
using Microsoft.EntityFrameworkCore;

public class SearchService
public class SearchService : CommonService, ISearchService
{
private readonly AppDbContext _dbContext;

public SearchService(AppDbContext dbContext)
: base(dbContext)
{
_dbContext = dbContext;
}
Expand Down
Loading

0 comments on commit 52c2196

Please sign in to comment.