Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…-leetwars into dev
  • Loading branch information
pospyra committed Aug 28, 2023
2 parents 9072197 + 68137e4 commit ddfc051
Show file tree
Hide file tree
Showing 49 changed files with 31,633 additions and 9,772 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/continuous-delivery.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: Continuous Delivery

on:
push:
branches: [ main ]
branches: [ dev ]
# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using LeetWars.Core.Common.DTO.Challenge;
using LeetWars.Core.Common.DTO.Filters;

namespace LeetWars.Core.BLL.Interfaces
{
public interface IChallengeService
{
Task<ICollection<ChallengePreviewDto>> GetChallengesAsync(ChallengesFiltersDto filters);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using LeetWars.Core.Common.DTO.Language;

namespace LeetWars.Core.BLL.Interfaces
{
public interface ILanguageService
{
Task<ICollection<LanguageDto>> GetAllLanguageAsync();

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using LeetWars.Core.Common.DTO.Tag;

namespace LeetWars.Core.BLL.Interfaces
{
public interface ITagService
{
Task<ICollection<TagDto>> GetAllTagsAsync();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using LeetWars.Core.Common.DTO.User;

namespace LeetWars.Core.BLL.Services;

public interface IUserService
{
public Task<UserDto> CreateUserAsync(NewUserDto userDto);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using AutoMapper;
using LeetWars.Core.Common.DTO.Challenge;
using LeetWars.Core.DAL.Entities;

namespace LeetWars.Core.BLL.MappingProfiles
{
public class ChallengeProfile : Profile
{
public ChallengeProfile()
{
CreateMap<Challenge, ChallengePreviewDto>()
.ForMember(dest => dest.AuthorName, opt => opt.MapFrom(src => src.Author != null ? $"{src.Author.FirstName} {src.Author.LastName}" : null))
.ForMember(dest => dest.Tags, opt => opt.MapFrom(src => src.Tags))
.ForMember(dest => dest.Title, opt => opt.MapFrom(src => src.Title))
.ForMember(dest => dest.Languages, opt => opt.MapFrom(src => src.Versions.Select(version => version.Language)))
.ForMember(dest => dest.Status, opt => opt.MapFrom(src => src.Versions.Any() ? src.Versions.First().Status : 0));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using AutoMapper;
using LeetWars.Core.Common.DTO.Language;
using LeetWars.Core.DAL.Entities;

namespace LeetWars.Core.BLL.MappingProfiles
{
public class LanguageProfile : Profile
{
public LanguageProfile()
{
CreateMap<Language, LanguageDto>().ReverseMap();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using AutoMapper;
using LeetWars.Core.Common.DTO;
using LeetWars.Core.Common.DTO.User;
using LeetWars.Core.DAL.Entities;

namespace LeetWars.Core.BLL.MappingProfiles
Expand All @@ -11,6 +12,8 @@ public SampleProfile()
CreateMap<Sample, SampleDto>();
CreateMap<SampleDto, Sample>();
CreateMap<NewSampleDto, Sample>();
CreateMap<User, UserDto>();
CreateMap<NewUserDto, User>();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using AutoMapper;
using LeetWars.Core.Common.DTO.Tag;
using LeetWars.Core.DAL.Entities;

namespace LeetWars.Core.BLL.MappingProfiles
{
public class TagProfile : Profile
{
public TagProfile()
{
CreateMap<Tag, TagDto>().ReverseMap();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
using AutoMapper;
using LeetWars.Core.BLL.Interfaces;
using LeetWars.Core.Common.DTO.Challenge;
using LeetWars.Core.Common.DTO.Filters;
using LeetWars.Core.DAL.Context;
using LeetWars.Core.DAL.Enums;
using Microsoft.EntityFrameworkCore;

namespace LeetWars.Core.BLL.Services
{
public class ChallengeService : BaseService, IChallengeService
{
public ChallengeService(LeetWarsCoreContext context, IMapper mapper) : base(context, mapper) { }

public async Task<ICollection<ChallengePreviewDto>> GetChallengesAsync(ChallengesFiltersDto filters)
{
var challenges = _context.Challenges
.Include(challenge => challenge.Tags)
.Include(challenge => challenge.Author)
.Include(challenge => challenge.Versions)
.ThenInclude(version => version.Language)
.Include(challenge => challenge.Versions)
.ThenInclude(version => version.Solutions)
.AsQueryable();

if (!string.IsNullOrEmpty(filters.Title))
{
challenges = challenges.Where(p => p.Title.ToLower().Contains(filters.Title.ToLower()));
}

if (filters.ChallengeStatus.HasValue)
{
challenges = challenges.Where(challenge =>
challenge.Versions.Any(version => version.Status == filters.ChallengeStatus));
}

if (filters.LanguageId.HasValue)
{
challenges = challenges.Where(challenge =>
challenge.Versions.Any(version => version.LanguageId == filters.LanguageId));
}

if (filters.TagsIds != null)
{
challenges = challenges.Where(challenge =>
challenge.Tags.Any(tag => filters.TagsIds.Contains(tag.Id)));
}

if (filters.Progress.HasValue)
{
challenges = filters.Progress switch
{
ChallengesProgress.NotStarted => challenges.Where(challenge => !challenge.Versions.Any(version => version.Solutions.Any())),
ChallengesProgress.Started => challenges.Where(challenge => challenge.Versions.Any(version => version.Solutions.Any())),
ChallengesProgress.Completed => challenges.Where(challenge => challenge.Versions.All(version =>
version.Solutions.All(solution => solution.SubmittedAt.HasValue && solution.SubmittedAt.Value != DateTime.MinValue))),
_ => challenges
};
}


var filteredChallenges = await challenges.ToListAsync();
return _mapper.Map<List<ChallengePreviewDto>>(filteredChallenges);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using AutoMapper;
using LeetWars.Core.BLL.Interfaces;
using LeetWars.Core.Common.DTO.Language;
using LeetWars.Core.DAL.Context;
using Microsoft.EntityFrameworkCore;

namespace LeetWars.Core.BLL.Services
{
public class LanguageService : BaseService, ILanguageService
{
public LanguageService(LeetWarsCoreContext context, IMapper mapper) : base(context, mapper)
{
}

public async Task<ICollection<LanguageDto>> GetAllLanguageAsync()
{
var languages = await _context.Languages.ToListAsync();

return _mapper.Map<ICollection<LanguageDto>>(languages);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
using AutoMapper;
using LeetWars.Core.BLL.Interfaces;
using LeetWars.Core.Common.DTO;
using LeetWars.Core.DAL.Context;
using LeetWars.Core.DAL.Entities;
using Microsoft.EntityFrameworkCore;

namespace LeetWars.Core.BLL.Services
{
Expand Down
22 changes: 22 additions & 0 deletions backend/LeetWars.Core/LeetWars.Core.BLL/Services/TagService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using AutoMapper;
using LeetWars.Core.BLL.Interfaces;
using LeetWars.Core.Common.DTO.Tag;
using LeetWars.Core.DAL.Context;
using Microsoft.EntityFrameworkCore;

namespace LeetWars.Core.BLL.Services
{
public class TagService : BaseService, ITagService
{
public TagService(LeetWarsCoreContext context, IMapper mapper) : base(context, mapper)
{
}

public async Task<ICollection<TagDto>> GetAllTagsAsync()
{
var tags = await _context.Tags.ToListAsync();

return _mapper.Map<ICollection<TagDto>>(tags);
}
}
}
34 changes: 34 additions & 0 deletions backend/LeetWars.Core/LeetWars.Core.BLL/Services/UserService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using AutoMapper;
using LeetWars.Core.Common.DTO.User;
using LeetWars.Core.DAL.Context;
using LeetWars.Core.DAL.Entities;
using Microsoft.EntityFrameworkCore;

namespace LeetWars.Core.BLL.Services;

public class UserService : BaseService, IUserService
{
public UserService(LeetWarsCoreContext context, IMapper mapper) : base(context, mapper)
{
}

public async Task<UserDto> CreateUserAsync(NewUserDto userDto)
{
if (userDto is null)
{
throw new ArgumentNullException(nameof(userDto));
}

var user = await _context.Users.FirstOrDefaultAsync(u => u.Uid == userDto.Uid);
if (user != null)
{
return _mapper.Map<UserDto>(user);
}

var newUser = _mapper.Map<NewUserDto, User>(userDto);
var createdUser = _context.Users.Add(newUser).Entity;
await _context.SaveChangesAsync();

return _mapper.Map<UserDto>(createdUser);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using LeetWars.Core.Common.DTO.Language;
using LeetWars.Core.Common.DTO.Tag;
using LeetWars.Core.DAL.Entities;

namespace LeetWars.Core.Common.DTO.Challenge
{
public class ChallengePreviewDto : Entity<long>
{
public string AuthorName { get; set; } = string.Empty;

public string Title { get; set; } = string.Empty;

public ICollection<TagDto>? Tags { get; set; }

public ICollection<LanguageDto>? Languages { get; set; }

public int Status { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using LeetWars.Core.DAL.Enums;

namespace LeetWars.Core.Common.DTO.Filters
{
public class ChallengesFiltersDto
{
public string? Title { get; set; }
public ChallengeStatus? ChallengeStatus { get; set; }
public int? LanguageId { get; set; }
public ICollection<int>? TagsIds { get; set; }
public ChallengesProgress? Progress { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,10 @@ namespace LeetWars.Core.Common.DTO.User
{
public class NewUserDto
{
public Country Country { get; set; }
public string Uid { get; set; } = string.Empty;
public int Timezone { get; set; }
public Sex Sex { get; set; }
public DateTime BirthDate { get; set; }
public string FirstName { get; set; } = string.Empty;
public string LastName { get; set; } = string.Empty;
public string UserName { get; set; } = string.Empty;
public string Email { get; set; } = string.Empty;
public string ImagePath { get; set; } = string.Empty;
public string OAuthToken { get; set; } = string.Empty;
public string? ImagePath { get; set; }
}
}
13 changes: 6 additions & 7 deletions backend/LeetWars.Core/LeetWars.Core.Common/DTO/User/UserDto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,18 @@ namespace LeetWars.Core.Common.DTO.User
{
public class UserDto : Entity<long>
{
public Country Country { get; set; }
public Country? Country { get; set; }
public int Timezone { get; set; }
public Sex Sex { get; set; }
public UserStatus Status { get; set; }
public DateTime BirthDate { get; set; }
public string FirstName { get; set; } = string.Empty;
public string LastName { get; set; } = string.Empty;
public DateTime? BirthDate { get; set; }
public string? FirstName { get; set; }
public string? LastName { get; set; }
public string UserName { get; set; } = string.Empty;
public string Email { get; set; } = string.Empty;
public string ImagePath { get; set; } = string.Empty;
public string Email { get; set; } = string.Empty;
public string? ImagePath { get; set; }
public long TotalScore { get; set; }
public DateTime RegisteredAt { get; set; }
public string OAuthToken { get; set; } = string.Empty;
public bool IsSubscribed { get; set; }
public bool IsBanned { get; set; }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ private static ICollection<User> GenerateUsers(int count = 40)
f.Random.AlphaNumeric(32)))
.UseSeed(SeedDefaults.UserSeed)
.RuleFor(e => e.Id, f => f.IndexGlobal)
.RuleFor(e=>e.Uid, f=> f.Random.AlphaNumeric(28))
.RuleFor(e => e.Country, f => f.PickRandom<Country>())
.RuleFor(e => e.Sex, f => f.PickRandom<Sex>())
.RuleFor(e => e.Status, f => f.PickRandom<UserStatus>())
Expand Down
4 changes: 2 additions & 2 deletions backend/LeetWars.Core/LeetWars.Core.DAL/Entities/Challenge.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ public class Challenge : AuditEntity<long>
public int LevelId { get; set; }
public ChallengeLevel? Level { get; set; }
public User? Author { get; set; }
public List<Tag> Tags { get; } = new();
public List<ChallengeVersion> Versions { get; } = new();
public ICollection<Tag> Tags { get; } = new List<Tag>();
public ICollection<ChallengeVersion> Versions { get; } = new List<ChallengeVersion>();

public Challenge(string title, string instructions)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ public class ChallengeVersion : AuditEntity<long>
public string InitialSolution { get; set; }
public string CompleteSolution { get; set; }
public ChallengeStatus Status { get; set; }
public List<LanguageVersion> LanguageVersions { get; } = new();
public List<UserSolution> Solutions { get; } = new();
public List<Test> Tests { get; } = new();
public ICollection<LanguageVersion> LanguageVersions { get; } = new List<LanguageVersion>();
public ICollection<UserSolution> Solutions { get; } = new List<UserSolution>();
public ICollection<Test> Tests { get; } = new List<Test>();
public Language? Language { get; set; }
public User? Author { get; set; }

Expand Down
4 changes: 2 additions & 2 deletions backend/LeetWars.Core/LeetWars.Core.DAL/Entities/Language.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
public class Language : Entity<int>
{
public string Name { get; set; }
public List<User> Users { get; } = new();
public List<LanguageVersion> LanguageVersions { get; } = new();
public ICollection<User> Users { get; } = new List<User>();
public ICollection<LanguageVersion> LanguageVersions { get; } = new List<LanguageVersion>();

public Language(string name)
{
Expand Down
Loading

0 comments on commit ddfc051

Please sign in to comment.