Skip to content

Commit

Permalink
201: API for getting post users
Browse files Browse the repository at this point in the history
  • Loading branch information
evan-scales committed Jan 23, 2024
1 parent 3f86764 commit 55584ce
Show file tree
Hide file tree
Showing 5 changed files with 148 additions and 0 deletions.
111 changes: 111 additions & 0 deletions FU.API/FU.API.Tests/PostServiceTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
namespace FU.API.Tests;

using FU.API.Data;
using FU.API.Models;
using FU.API.Services;
using Microsoft.EntityFrameworkCore;

public class PostServiceTests
{
private readonly DbContextOptions<AppDbContext> _contextOptions;

// adapted from https://github.com/dotnet/EntityFramework.Docs/blob/main/samples/core/Testing/TestingWithoutTheDatabase/InMemoryBloggingControllerTest.cs
public PostServiceTests()
{
_contextOptions = new DbContextOptionsBuilder<AppDbContext>()
.UseInMemoryDatabase(Guid.NewGuid().ToString())
.Options;
}

private AppDbContext CreateContext()
{
var context = new AppDbContext(_contextOptions);

var testUsers = CreateTestUsers();
var testChat = CreateTestChat(testUsers);
context.Set<Chat>().Add(testChat);
context.Set<ApplicationUser>().AddRange(testUsers);

var post = new Post()
{
Id = 1,
Title = "TestTitle",
GameId = 1,
CreatorId = testUsers[0].UserId,
Creator = testUsers[0],
ChatId = testChat.Id,
Chat = testChat,
};

context.Set<Post>().Add(post);
context.SaveChanges();

return context;
}

// Test for GetPostUsers method

[Theory]
[InlineData(1, 1, true)]
[InlineData(1, 2, true)]
[InlineData(1, 5, false)]
public async void GetPostUsers_WithValidPostId_CheckUserJoined(int postId, int checkUserId, bool expectedJoined)
{
// Arrange
var context = CreateContext();
var chatService = new ChatService(context);
var postService = new PostService(context, chatService);

// Act
var postUsers = await postService.GetPostUsers(postId);

// Assert
var joined = postUsers.Any(u => u.UserId == checkUserId);
Assert.Equal(expectedJoined, joined);
Assert.Equal(4, postUsers.Count());
}

private List<ApplicationUser> CreateTestUsers()
{
return new List<ApplicationUser>()
{
new ApplicationUser()
{
UserId = 1,
Username = "User1",
},
new ApplicationUser()
{
UserId = 2,
Username = "User2",
},
new ApplicationUser()
{
UserId = 3,
Username = "User3",
},
new ApplicationUser()
{
UserId = 4,
Username = "User4",
},
};
}

private Chat CreateTestChat(List<ApplicationUser> users)
{
return new Chat()
{
Id = 1,
ChatType = ChatType.Post,
ChatName = "Title1",
CreatorId = 1,
Members = users.Select(u => new ChatMembership()
{
ChatId = 1,
UserId = u.UserId,
User = u,
}).ToList(),
};
}
}
19 changes: 19 additions & 0 deletions FU.API/FU.API/Controllers/PostsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,23 @@ public async Task<IActionResult> LeavePost(int postId)

return NoContent();
}

[HttpGet]
[Route("{postId}/users")]
[AllowAnonymous]
public async Task<IActionResult> GetPostUsers(int postId)
{
var post = await _postService.GetPost(postId);

if (post is null)
{
return NotFound();
}

var users = await _postService.GetPostUsers(post.Id);

var response = users.ToProfiles();

return Ok(response);
}
}
3 changes: 3 additions & 0 deletions FU.API/FU.API/Helpers/Mapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ public static UserProfile ToProfile(this ApplicationUser appUser)
};
}

public static IEnumerable<UserProfile> ToProfiles(this IEnumerable<ApplicationUser> appUsers) =>
appUsers.Select(appUser => appUser.ToProfile());

public static MessageResponseDTO ToDto(this Message message)
{
return new MessageResponseDTO()
Expand Down
2 changes: 2 additions & 0 deletions FU.API/FU.API/Interfaces/IPostService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,6 @@ public interface IPostService : ICommonService
Task JoinPost(int postId, ApplicationUser user);

Task LeavePost(int postId, ApplicationUser user);

Task<IEnumerable<ApplicationUser>> GetPostUsers(int postId);
}
13 changes: 13 additions & 0 deletions FU.API/FU.API/Services/PostService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ namespace FU.API.Services;
using FU.API.Interfaces;
using FU.API.Models;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;

public class PostService : CommonService, IPostService
{
Expand Down Expand Up @@ -67,6 +68,18 @@ public async Task<Post> CreatePost(Post post)
.FirstOrDefaultAsync();
}

public async Task<IEnumerable<ApplicationUser>> GetPostUsers(int postId)
{
return await _dbContext.Posts
.Where(p => p.Id == postId)
.Include(p => p.Chat)
.ThenInclude(c => c.Members)
.ThenInclude(cm => cm.User)
.SelectMany(p => p.Chat.Members)
.Select(cm => cm.User)
.ToListAsync();
}

public async Task JoinPost(int postId, ApplicationUser user)
{
var post = await _dbContext.Posts
Expand Down

0 comments on commit 55584ce

Please sign in to comment.