Skip to content

Commit

Permalink
Formatting
Browse files Browse the repository at this point in the history
Co-Authored-By: RasmusLarsen02 <105587694+RasmusLarsen02@users.noreply.github.com>
  • Loading branch information
math045b and RasmusLarsen02 committed Nov 28, 2024
1 parent 17304b7 commit e1f9503
Show file tree
Hide file tree
Showing 18 changed files with 163 additions and 161 deletions.
8 changes: 4 additions & 4 deletions Chirp/src/Chirp.Core/AuthorService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Chirp.Core;


public interface IAuthorService
{
public List<AuthorDTO> GetFollows(string username);
Expand All @@ -11,13 +10,14 @@ public interface IAuthorService
public bool Unfollow(string currentUser, string userToUnFollow);
public bool MakeFollowersUnfollow(string username);
}

public class AuthorService(IAuthorRepository db) : IAuthorService
{
public List<AuthorDTO> GetFollows(string username)
{
return db.GetAuthorFollows(username).Result;
}

public List<AuthorDTO> GetFollowers(string username)
{
return db.GetAuthorFollowers(username).Result;
Expand All @@ -27,12 +27,12 @@ public bool MakeFollowersUnfollow(string username)
{
return db.MakeFollowersUnfollow(username).Result;
}

public bool Follow(string currentUser, string userToFollow)
{
return db.Follow(currentUser, userToFollow).Result;
}

public bool Unfollow(string currentUser, string userToUnFollow)
{
return db.UnFollow(currentUser, userToUnFollow).Result;
Expand Down
7 changes: 4 additions & 3 deletions Chirp/src/Chirp.Core/CheepService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ public List<CheepDTO> GetCheepsByPage(int page, int pageSize)
{
ArgumentOutOfRangeException.ThrowIfNegative(pageSize);

var result = db.GetCheepsByPage(page, pageSize).Result ?? throw new ArgumentNullException(nameof(db.GetCheepsByPage));

var result = db.GetCheepsByPage(page, pageSize).Result ??
throw new ArgumentNullException(nameof(db.GetCheepsByPage));

return result.ToList();
}

Expand All @@ -37,7 +38,7 @@ public List<CheepDTO> GetCheepsFromAuthorByPage(string author, int page, int pag
{
return db.GetCheepsFromAuthorByPage(author, page, pageSize).Result.ToList();
}

public List<CheepDTO> GetCheepsFromAuthorsByPage(IEnumerable<string> authors, int page, int pageSize)
{
return db.GetCheepsFromAuthorsByPage(authors, page, pageSize).Result.ToList();
Expand Down
2 changes: 1 addition & 1 deletion Chirp/src/Chirp.Core/DTO/CommentDTO.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace Chirp.Core.DTO;

public class CommentDTO (string author, int cheepId, string message, long unixTimestamp)
public class CommentDTO(string author, int cheepId, string message, long unixTimestamp)
{
public string Author { get; set; } = author;
public int CheepId { get; set; } = cheepId;
Expand Down
4 changes: 2 additions & 2 deletions Chirp/src/Chirp.Core/IAuthorRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public interface IAuthorRepository
public Task<bool> AddAuthor(AuthorDTO author);
public Task<List<AuthorDTO>> GetAuthorFollows(string username);
public Task<List<AuthorDTO>> GetAuthorFollowers(string username);
public Task<bool> Follow (string currentUser, string userToFollow);
public Task<bool> UnFollow (string currentUser, string userToUnFollow);
public Task<bool> Follow(string currentUser, string userToFollow);
public Task<bool> UnFollow(string currentUser, string userToUnFollow);
public Task<bool> MakeFollowersUnfollow(string user);
}
22 changes: 11 additions & 11 deletions Chirp/src/Chirp.Infrastructure/AuthorRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public async Task<List<AuthorDTO>> GetAuthorFollowers(string username)
.Where(a => a.NormalizedUserName == username.ToUpper())
.Select(a => a.Followers)
.FirstOrDefaultAsync() ?? new List<Author>();

return followers.Select(a => new AuthorDTO(a.UserName!, a.Email!)).ToList();
}

Expand All @@ -53,10 +53,10 @@ public async Task<bool> Follow(string currentUser, string userToFollow)
if (!await UserExists(currentUser)) throw new UserDoesNotExist();
if (!await UserExists(userToFollow)) throw new UserDoesNotExist("User to follow does not exist");
if (await DoesFollow(currentUser, userToFollow)) return false;

var authorToFollow = await GetAuthor(userToFollow);
GetAuthor(currentUser).Result.Following.Add(authorToFollow);

await context.SaveChangesAsync();
return true;
}
Expand All @@ -69,17 +69,17 @@ public async Task<bool> UnFollow(string currentUser, string userToUnfollow)
if (!await UserExists(currentUser)) throw new UserDoesNotExist();
if (!await UserExists(userToUnfollow)) throw new UserDoesNotExist("User to unfollow does not exist");
if (!await DoesFollow(currentUser, userToUnfollow)) return false;

var authorToUnfollow = await GetAuthor(userToUnfollow);
context.Authors
.Where(a => a.NormalizedUserName == currentUser)
.Include(a => a.Following)
.FirstOrDefault()!.Following.Remove(authorToUnfollow);

await context.SaveChangesAsync();
return true;
}

private async Task<Author> GetAuthor(string username)
{
username = username.ToUpper();
Expand All @@ -98,11 +98,11 @@ private async Task<bool> DoesFollow(string currentUser, string userToFollow)
{
currentUser = currentUser.ToUpper();
userToFollow = userToFollow.ToUpper();
var list = await context.Authors
.Where(a => a.NormalizedUserName == currentUser)
.Select(a => a.Following).FirstOrDefaultAsync();
return list != null && list.Any(a => a.NormalizedUserName == userToFollow);
var list = await context.Authors
.Where(a => a.NormalizedUserName == currentUser)
.Select(a => a.Following).FirstOrDefaultAsync();

return list != null && list.Any(a => a.NormalizedUserName == userToFollow);
}

private async Task<IEnumerable<Author>> GetFollowing(string currentUser)
Expand Down
43 changes: 29 additions & 14 deletions Chirp/src/Chirp.Infrastructure/CheepRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,18 @@ public class CheepRepository(ChirpDBContext context) : ICheepRepository
public async Task<IEnumerable<CheepDTO>?> GetCheepsByPage(int page, int pageSize)
{
if (pageSize < 0) return null;

var query = context.Cheeps
.Select(cheep => new {cheep.Id, cheep.Author.UserName, cheep.Message, cheep.TimeStamp })
.Select(cheep => new { cheep.Id, cheep.Author.UserName, cheep.Message, cheep.TimeStamp })
.OrderByDescending(cheep => cheep.TimeStamp)
.Skip((page - 1) * pageSize)
.Take(pageSize);

var cheeps = await query.ToListAsync();

return cheeps.Select(cheep =>
new CheepDTO(cheep.Id, cheep.UserName!, cheep.Message, new DateTimeOffset(cheep.TimeStamp).ToUnixTimeSeconds()));
new CheepDTO(cheep.Id, cheep.UserName!, cheep.Message,
new DateTimeOffset(cheep.TimeStamp).ToUnixTimeSeconds()));
}

public async Task<IEnumerable<CheepDTO>> GetCheepsFromAuthorByPage(string author, int page, int pageSize)
Expand All @@ -40,10 +41,12 @@ public async Task<IEnumerable<CheepDTO>> GetCheepsFromAuthor(string author)
var cheeps = await query.ToListAsync();

return cheeps.Select(cheep =>
new CheepDTO(cheep.Id, cheep.UserName!, cheep.Message, new DateTimeOffset(cheep.TimeStamp).ToUnixTimeSeconds()));
new CheepDTO(cheep.Id, cheep.UserName!, cheep.Message,
new DateTimeOffset(cheep.TimeStamp).ToUnixTimeSeconds()));
}

public async Task<IEnumerable<CheepDTO>> GetCheepsFromAuthorsByPage(IEnumerable<string> authors, int page, int pageSize)

public async Task<IEnumerable<CheepDTO>> GetCheepsFromAuthorsByPage(IEnumerable<string> authors, int page,
int pageSize)
{
authors = authors.Select(author => author.ToUpper());
var query = context.Cheeps
Expand All @@ -56,7 +59,8 @@ public async Task<IEnumerable<CheepDTO>> GetCheepsFromAuthorsByPage(IEnumerable<
var cheeps = await query.ToListAsync();

return cheeps.Select(cheep =>
new CheepDTO(cheep.Id, cheep.UserName!, cheep.Message, new DateTimeOffset(cheep.TimeStamp).ToUnixTimeSeconds()));
new CheepDTO(cheep.Id, cheep.UserName!, cheep.Message,
new DateTimeOffset(cheep.TimeStamp).ToUnixTimeSeconds()));
}

public Task<int> GetAmountOfCheeps()
Expand All @@ -77,7 +81,11 @@ public async Task<bool> CreateCheep(CheepDTO cheep)
.Where(a => a.UserName == cheep.Author)
.FirstOrDefaultAsync();
if (author == null) return false;
var cheep2 = new Cheep {Author = author, Message = cheep.Message, TimeStamp = DateTimeOffset.FromUnixTimeSeconds(cheep.UnixTimestamp).DateTime};
var cheep2 = new Cheep
{
Author = author, Message = cheep.Message,
TimeStamp = DateTimeOffset.FromUnixTimeSeconds(cheep.UnixTimestamp).DateTime
};
context.Cheeps.Add(cheep2);
await context.SaveChangesAsync();
return true;
Expand All @@ -94,15 +102,19 @@ public async Task<bool> AddCommentToCheep(CommentDTO comment)
.Include(c => c.Comments)
.FirstOrDefaultAsync();
if (cheep == null) return false;
var newComment = new Comment {Author = author, Cheep = cheep, Message = comment.Message, TimeStamp = DateTimeOffset.FromUnixTimeSeconds(comment.UnixTimestamp).DateTime};
var newComment = new Comment
{
Author = author, Cheep = cheep, Message = comment.Message,
TimeStamp = DateTimeOffset.FromUnixTimeSeconds(comment.UnixTimestamp).DateTime
};
cheep.Comments.Add(newComment);
await context.SaveChangesAsync();
return true;
return true;
}

public async Task<int> GetCommentAmountOnCheep(int? cheepId)
{
if (cheepId == null) return 0;
if (cheepId == null) return 0;
var query = await context.Cheeps
.Include(c => c.Comments)
.Where(c => c.Id == cheepId)
Expand All @@ -117,8 +129,10 @@ public async Task<CheepDTO> GetCheepById(int cheepId)
.Where(c => c.Id == cheepId)
.Select(cheep => new { cheep.Id, cheep.Author.UserName, cheep.Message, cheep.TimeStamp })
.FirstOrDefaultAsync();

return new CheepDTO(cheep!.Id, cheep.UserName!, cheep.Message, new DateTimeOffset(cheep.TimeStamp).ToUnixTimeSeconds());;

return new CheepDTO(cheep!.Id, cheep.UserName!, cheep.Message,
new DateTimeOffset(cheep.TimeStamp).ToUnixTimeSeconds());
;
}

public async Task<IEnumerable<CommentDTO>> GetCommentsForCheep(int cheepId)
Expand All @@ -130,6 +144,7 @@ public async Task<IEnumerable<CommentDTO>> GetCommentsForCheep(int cheepId)
.FirstOrDefaultAsync();
var comments = query!.Comments.OrderByDescending(c => c.TimeStamp);
return comments.Select(comment =>
new CommentDTO(comment.Author.UserName!, comment.Id, comment.Message, new DateTimeOffset(comment.TimeStamp).ToUnixTimeSeconds()));
new CommentDTO(comment.Author.UserName!, comment.Id, comment.Message,
new DateTimeOffset(comment.TimeStamp).ToUnixTimeSeconds()));
}
}
6 changes: 3 additions & 3 deletions Chirp/src/Chirp.Infrastructure/Chirp.Infrastructure.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\Chirp.Core\Chirp.Core.csproj" />
<ProjectReference Include="..\Chirp.Core\Chirp.Core.csproj"/>
</ItemGroup>

<ItemGroup>
<PackageReference Include="FluentValidation" Version="11.10.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="8.0.10" />
<PackageReference Include="FluentValidation" Version="11.10.0"/>
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="8.0.10"/>
</ItemGroup>

</Project>
8 changes: 3 additions & 5 deletions Chirp/src/Chirp.Infrastructure/ChirpDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ public class ChirpDBContext(DbContextOptions<ChirpDBContext> options) : Identity
{
public DbSet<Cheep> Cheeps { get; set; }
public DbSet<Author> Authors { get; set; }

public DbSet<Comment> Comments { get; set; }

protected override void OnModelCreating(ModelBuilder modelBuilder)
Expand Down Expand Up @@ -52,14 +51,14 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
entity.HasMany(a => a.Following)
.WithMany(a => a.Followers);
});

//define comment entity constraints
modelBuilder.Entity<Comment>(entity =>
{
entity.HasKey((c => c.Id));
entity.Property(c => c.Id)
.ValueGeneratedOnAdd();

entity.Property(c => c.Message)
.IsRequired()
.HasMaxLength(160);
Expand All @@ -69,12 +68,11 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
.WithMany(a => a.Comments)
.IsRequired()
.OnDelete(DeleteBehavior.Cascade);

entity.HasOne(c => c.Cheep)
.WithMany(c => c.Comments)
.IsRequired()
.OnDelete(DeleteBehavior.Cascade);
});

}
}
1 change: 0 additions & 1 deletion Chirp/src/Chirp.Infrastructure/Model/Author.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ public sealed class Author : IdentityUser
public List<Cheep> Cheeps { get; set; } = [];
public List<Author> Following { get; set; } = [];
public List<Author> Followers { get; set; } = [];

public List<Comment> Comments { get; set; } = [];

public Author()
Expand Down
9 changes: 4 additions & 5 deletions Chirp/src/Chirp.Infrastructure/Model/Cheep.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@ namespace Chirp.Infrastructure.Model;

public class Cheep
{
public int Id { get; set; }
public required string Message { get; set; }
public DateTime TimeStamp { get; set; }
public int Id { get; set; }
public required string Message { get; set; }
public DateTime TimeStamp { get; set; }
public required Author Author { get; set; }

public List<Comment> Comments { get; set; } = [];

public Cheep()
{

}

public Cheep(int id, string message, DateTime timeStamp, Author author)
Expand Down
3 changes: 1 addition & 2 deletions Chirp/src/Chirp.Infrastructure/Model/Comment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@ public class Comment
public int Id { get; set; }
public required Author Author { get; set; }
public required Cheep Cheep { get; set; }
public DateTime TimeStamp { get; set; }
public DateTime TimeStamp { get; set; }
public required string Message { get; set; }

public Comment()
{

}

public Comment(int id, Author author, Cheep cheep, string message, DateTime timeStamp)
Expand Down
Loading

0 comments on commit e1f9503

Please sign in to comment.