Skip to content

Commit 83fa748

Browse files
committed
Merge branch 'main' into like-tweets
2 parents e8d7338 + 7dcdff1 commit 83fa748

Some content is hidden

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

46 files changed

+1630
-209
lines changed

Chirp/src/Chirp.Core/AuthorService.cs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,23 @@ namespace Chirp.Core;
44

55
public interface IAuthorService
66
{
7-
public List<AuthorDTO> GetFollows(string username);
8-
public List<AuthorDTO> GetFollowers(string username);
7+
public IEnumerable<AuthorDTO> GetFollows(string username);
8+
public IEnumerable<AuthorDTO> GetFollowers(string username);
99
public bool Follow(string currentUser, string userToFollow);
1010
public bool Unfollow(string currentUser, string userToUnFollow);
1111
public bool MakeFollowersUnfollow(string username);
12+
public IEnumerable<CommentDTO> GetComments(string username);
13+
public IEnumerable<AuthorDTO?> GetAuthorsByNames(IEnumerable<String> names);
1214
}
1315

1416
public class AuthorService(IAuthorRepository db) : IAuthorService
1517
{
16-
public List<AuthorDTO> GetFollows(string username)
18+
public IEnumerable<AuthorDTO> GetFollows(string username)
1719
{
1820
return db.GetAuthorFollows(username).Result;
1921
}
2022

21-
public List<AuthorDTO> GetFollowers(string username)
23+
public IEnumerable<AuthorDTO> GetFollowers(string username)
2224
{
2325
return db.GetAuthorFollowers(username).Result;
2426
}
@@ -28,6 +30,11 @@ public bool MakeFollowersUnfollow(string username)
2830
return db.MakeFollowersUnfollow(username).Result;
2931
}
3032

33+
public IEnumerable<CommentDTO> GetComments(string username)
34+
{
35+
return db.GetComments(username).Result;
36+
}
37+
3138
public bool Follow(string currentUser, string userToFollow)
3239
{
3340
return db.Follow(currentUser, userToFollow).Result;
@@ -37,4 +44,9 @@ public bool Unfollow(string currentUser, string userToUnFollow)
3744
{
3845
return db.UnFollow(currentUser, userToUnFollow).Result;
3946
}
47+
48+
public IEnumerable<AuthorDTO?> GetAuthorsByNames(IEnumerable<String> names)
49+
{
50+
return db.GetAuthorsByNames(names).Result;
51+
}
4052
}

Chirp/src/Chirp.Core/CheepService.cs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,17 @@ namespace Chirp.Core;
44

55
public interface ICheepService
66
{
7-
public List<CheepDTO> GetCheepsByPage(int page, int pageSize);
8-
public List<CheepDTO> GetCheepsFromAuthorByPage(string author, int page, int pageSize);
9-
public List<CheepDTO> GetCheepsFromAuthorsByPage(IEnumerable<string> authors, int page, int pageSize);
10-
public List<CheepDTO> GetCheepsFromAuthor(string author);
7+
public IEnumerable<CheepDTO> GetCheepsByPage(int page, int pageSize);
8+
public IEnumerable<CheepDTO> GetCheepsFromAuthorByPage(string author, int page, int pageSize);
9+
public IEnumerable<CheepDTO> GetCheepsFromAuthorsByPage(IEnumerable<string> authors, int page, int pageSize);
10+
public IEnumerable<CheepDTO> GetCheepsFromAuthor(string author);
1111
public int GetAmountOfCheepPages(int pageSize);
1212
public int GetAmountOfCheepPagesFromAuthors(IEnumerable<String> authors, int pageSize);
1313
public bool CreateCheep(CheepDTO cheep);
1414
public bool AddCommentToCheep(CommentDTO comment);
1515
public int GetCommentAmountOnCheep(int? cheepId);
1616
public CheepDTO GetCheepFromId(int cheepId);
17-
public List<CommentDTO> GetCommentsFromCheep(int cheepId);
18-
17+
public IEnumerable<CommentDTO> GetCommentsFromCheep(int cheepId);
1918
public bool LikeCheep(LikeDTO like);
2019
public bool UnlikeCheep(LikeDTO like);
2120
public Task<int> GetLikeCount(int cheepId);
@@ -24,7 +23,7 @@ public interface ICheepService
2423

2524
public class CheepService(ICheepRepository db) : ICheepService
2625
{
27-
public List<CheepDTO> GetCheepsByPage(int page, int pageSize)
26+
public IEnumerable<CheepDTO> GetCheepsByPage(int page, int pageSize)
2827
{
2928
ArgumentOutOfRangeException.ThrowIfNegative(pageSize);
3029

@@ -34,17 +33,17 @@ public List<CheepDTO> GetCheepsByPage(int page, int pageSize)
3433
return result.ToList();
3534
}
3635

37-
public List<CheepDTO> GetCheepsFromAuthor(string author)
36+
public IEnumerable<CheepDTO> GetCheepsFromAuthor(string author)
3837
{
3938
return db.GetCheepsFromAuthor(author).Result.ToList();
4039
}
4140

42-
public List<CheepDTO> GetCheepsFromAuthorByPage(string author, int page, int pageSize)
41+
public IEnumerable<CheepDTO> GetCheepsFromAuthorByPage(string author, int page, int pageSize)
4342
{
4443
return db.GetCheepsFromAuthorByPage(author, page, pageSize).Result.ToList();
4544
}
4645

47-
public List<CheepDTO> GetCheepsFromAuthorsByPage(IEnumerable<string> authors, int page, int pageSize)
46+
public IEnumerable<CheepDTO> GetCheepsFromAuthorsByPage(IEnumerable<string> authors, int page, int pageSize)
4847
{
4948
return db.GetCheepsFromAuthorsByPage(authors, page, pageSize).Result.ToList();
5049
}
@@ -79,7 +78,7 @@ public CheepDTO GetCheepFromId(int cheepId)
7978
return db.GetCheepById(cheepId).Result;
8079
}
8180

82-
public List<CommentDTO> GetCommentsFromCheep(int cheepId)
81+
public IEnumerable<CommentDTO> GetCommentsFromCheep(int cheepId)
8382
{
8483
return db.GetCommentsForCheep(cheepId).Result.ToList();
8584
}

Chirp/src/Chirp.Core/DTO/AuthorDTO.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
namespace Chirp.Core.DTO;
22

3-
public class AuthorDTO(string name, string email)
3+
public class AuthorDTO(string name, string email, byte[]? ProfileImage = null)
44
{
55
public string Name { get; set; } = name;
66
public string Email { get; set; } = email;
7+
public byte[]? ProfileImage { get; set; } = ProfileImage;
78
}

Chirp/src/Chirp.Core/IAuthorRepository.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@ namespace Chirp.Core;
55
public interface IAuthorRepository
66
{
77
public Task<AuthorDTO?> GetAuthorByName(string name);
8+
public Task<IEnumerable<AuthorDTO?>> GetAuthorsByNames(IEnumerable<string> names);
89
public Task<bool> AddAuthor(AuthorDTO author);
9-
public Task<List<AuthorDTO>> GetAuthorFollows(string username);
10-
public Task<List<AuthorDTO>> GetAuthorFollowers(string username);
10+
public Task<IEnumerable<AuthorDTO>> GetAuthorFollows(string username);
11+
public Task<IEnumerable<AuthorDTO>> GetAuthorFollowers(string username);
1112
public Task<bool> Follow(string currentUser, string userToFollow);
1213
public Task<bool> UnFollow(string currentUser, string userToUnFollow);
1314
public Task<bool> MakeFollowersUnfollow(string user);
15+
public Task<IEnumerable<CommentDTO>> GetComments(string username);
1416
}

Chirp/src/Chirp.Infrastructure/AuthorRepository.cs

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,15 @@ public class AuthorRepository(ChirpDBContext context) : IAuthorRepository
1313
public async Task<AuthorDTO?> GetAuthorByName(string name)
1414
{
1515
var author = await GetAuthor(name);
16-
return new AuthorDTO(author.UserName!, author.Email!);
16+
return new AuthorDTO(author.UserName!, author.Email!, author.ProfileImage!);
17+
}
18+
19+
public async Task<IEnumerable<AuthorDTO?>> GetAuthorsByNames(IEnumerable<string> names)
20+
{
21+
return await context.Authors
22+
.Where(a => names.Contains(a.UserName))
23+
.Select(a => new AuthorDTO(a.UserName!, a.Email!, a.ProfileImage))
24+
.ToListAsync();
1725
}
1826

1927
public async Task<bool> AddAuthor(AuthorDTO author)
@@ -30,21 +38,21 @@ public async Task<bool> AddAuthor(AuthorDTO author)
3038
}
3139
}
3240

33-
public async Task<List<AuthorDTO>> GetAuthorFollows(string username)
41+
public async Task<IEnumerable<AuthorDTO>> GetAuthorFollows(string username)
3442
{
3543
if (!await UserExists(username)) throw new UserDoesNotExist();
36-
return GetFollowing(username).Result.Select(a => new AuthorDTO(a.UserName!, a.Email!)).ToList();
44+
return GetFollowing(username).Result.Select(a => new AuthorDTO(a.UserName!, a.Email!, a.ProfileImage)).ToList();
3745
}
3846

39-
public async Task<List<AuthorDTO>> GetAuthorFollowers(string username)
47+
public async Task<IEnumerable<AuthorDTO>> GetAuthorFollowers(string username)
4048
{
4149
if (!await UserExists(username)) throw new UserDoesNotExist();
4250
var followers = await context.Authors
4351
.Where(a => a.NormalizedUserName == username.ToUpper())
4452
.Select(a => a.Followers)
4553
.FirstOrDefaultAsync() ?? new List<Author>();
4654

47-
return followers.Select(a => new AuthorDTO(a.UserName!, a.Email!)).ToList();
55+
return followers.Select(a => new AuthorDTO(a.UserName!, a.Email!, a.ProfileImage)).ToList();
4856
}
4957

5058
public async Task<bool> Follow(string currentUser, string userToFollow)
@@ -122,4 +130,18 @@ public async Task<bool> MakeFollowersUnfollow(string user)
122130
await context.SaveChangesAsync();
123131
return true;
124132
}
133+
134+
public async Task<IEnumerable<CommentDTO>> GetComments(string username)
135+
{
136+
if (!await UserExists(username)) throw new UserDoesNotExist();
137+
var query = context.Authors
138+
.Where(a => a.NormalizedUserName == username.ToUpper())
139+
.Include(a => a.Comments)
140+
.ThenInclude(c => c.Author)
141+
.Select(a => a.Comments)
142+
.FirstOrDefaultAsync();
143+
var comments = query.Result;
144+
145+
return comments!.Select(c => new CommentDTO(c.Author.UserName!, c.Id, c.Message, new DateTimeOffset(c.TimeStamp).ToUnixTimeSeconds())).ToList();
146+
}
125147
}

0 commit comments

Comments
 (0)