Skip to content

Commit

Permalink
Wip: languages
Browse files Browse the repository at this point in the history
  • Loading branch information
frpouly committed Feb 2, 2024
1 parent 74dcc20 commit 54280dd
Show file tree
Hide file tree
Showing 10 changed files with 3,287 additions and 3,252 deletions.
27 changes: 16 additions & 11 deletions src/Expressio/Controllers/ExpressionsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,40 @@

namespace Expressio.Controllers
{
[Route("api/[controller]")]
[Route("api/{lang}/[controller]")]
[ApiController]
public class ExpressionsController : ControllerBase
{
private readonly ExpressionContext _context;
private readonly ExpressioContext _context;

public ExpressionsController(ExpressionContext context)
public ExpressionsController(ExpressioContext context)
{
context.Database.EnsureCreated();
_context = context;
}

// GET: api/Expressions
[HttpGet]
public async Task<ActionResult<IEnumerable<Expression>>> GetExpressions()
public async Task<ActionResult<IEnumerable<ExpressionDTO>>> GetExpressions(string lang)
{
return await _context.Expressions.ToListAsync();
return await _context
.Expressions
.Include(e => e.Language)
.Select(e =>
new ExpressionDTO() {
Id = e.Id,
Content = e.Content,
Definitions = e.Definitions
}
)
.ToListAsync();
}

// GET: api/Expressions/5
[HttpGet("{id}")]
public async Task<ActionResult<Expression>> GetExpression(long id)

Check warning on line 43 in src/Expressio/Controllers/ExpressionsController.cs

View workflow job for this annotation

GitHub Actions / build

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.

Check warning on line 43 in src/Expressio/Controllers/ExpressionsController.cs

View workflow job for this annotation

GitHub Actions / build

This async method lacks 'await' operators and will run synchronously. Consider using the 'await' operator to await non-blocking API calls, or 'await Task.Run(...)' to do CPU-bound work on a background thread.
{
var Expression = await _context.Expressions.FindAsync(id);
var Expression = _context.Languages.First().Expressions.ToList().Find(e => e.Id == id);

if (Expression == null)
{
Expand All @@ -41,10 +51,5 @@ public async Task<ActionResult<Expression>> GetExpression(long id)

return Expression;
}

private bool ExpressionExists(long id)
{
return _context.Expressions.Any(e => e.Id == id);
}
}
}
6 changes: 3 additions & 3 deletions src/Expressio/Controllers/MixedExpressionsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ namespace Expressio.Controllers
[ApiController]
public class MixedExpressionsController : ControllerBase
{
private readonly ExpressionContext _context;
private readonly ExpressioContext _context;
private readonly Mixer _mixer;

public MixedExpressionsController(ExpressionContext context)
public MixedExpressionsController(ExpressioContext context)
{
context.Database.EnsureCreated();
_context = context;
_mixer = new Mixer(_context.Expressions.ToList());
_mixer = new Mixer(_context.Languages.First().Expressions.ToList());
}

[HttpGet("random")]
Expand Down
34 changes: 34 additions & 0 deletions src/Expressio/Models/ExpressioContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using Microsoft.EntityFrameworkCore;

namespace Expressio.Models;

public class ExpressioContext : DbContext
{
public ExpressioContext(DbContextOptions<ExpressioContext> options)
: base(options)
{
}

public ExpressioContext()
: base()
{
}

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Language>()
.HasMany(e => e.Expressions)
.WithOne(e => e.Language)
.HasForeignKey("LanguageId")
.IsRequired();
modelBuilder.Entity<Language>().HasData(
new Language(){ Id = 1, Code = "fr" }
);
modelBuilder.Entity<Expression>().HasData(
new FileLoader("Resources/expressions_fr.json").Load().Skip(3000)
);
}

public virtual DbSet<Language> Languages { get; set; }
public virtual DbSet<Expression> Expressions { get; set; }
}
6 changes: 5 additions & 1 deletion src/Expressio/Models/Expression.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using Humanizer;

namespace Expressio.Models;
Expand All @@ -6,7 +8,9 @@ public class Expression
{
public long Id { get; set; }
public required string Content { get; set; }
public string[]? Definitions { get; set; }
public List<string>? Definitions { get; set; }
public long LanguageId { get; set; }
public Language Language { get; set; }

Check warning on line 13 in src/Expressio/Models/Expression.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'Language' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 13 in src/Expressio/Models/Expression.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'Language' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

public IEnumerable<string> Words()
{
Expand Down
26 changes: 0 additions & 26 deletions src/Expressio/Models/ExpressionContext.cs

This file was deleted.

6 changes: 6 additions & 0 deletions src/Expressio/Models/ExpressionDTO.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
public class ExpressionDTO
{
public long Id { get; set; }
public required string Content { get; set; }
public List<string>? Definitions { get; set; }
}
2 changes: 1 addition & 1 deletion src/Expressio/Models/FileLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public FileLoader(string path)
Path = path;
}

public IEnumerable<Expression> Load()
public List<Expression> Load()
{
using (StreamReader r = new StreamReader(Path))
{
Expand Down
12 changes: 12 additions & 0 deletions src/Expressio/Models/Language.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using Expressio.Controllers;
using Microsoft.EntityFrameworkCore;

namespace Expressio.Models;

public class Language
{
public long Id { get; set; }
public required string Code { get; set; }
public virtual ICollection<Expression> Expressions { get; set; }

Check warning on line 10 in src/Expressio/Models/Language.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'Expressions' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 10 in src/Expressio/Models/Language.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'Expressions' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

}
2 changes: 1 addition & 1 deletion src/Expressio/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
var builder = WebApplication.CreateBuilder(args);

builder.Services.AddControllers();
builder.Services.AddDbContext<ExpressionContext>(opt => opt.UseInMemoryDatabase("Expressio"));
builder.Services.AddDbContext<ExpressioContext>(opt => opt.UseInMemoryDatabase("Expressio"));
builder.Services.AddEndpointsApiExplorer();

builder.Services.AddSwaggerGen();
Expand Down
Loading

0 comments on commit 54280dd

Please sign in to comment.