Skip to content

Commit

Permalink
user and mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
Logerfo committed May 8, 2020
1 parent 11e7fdf commit 14f936a
Show file tree
Hide file tree
Showing 12 changed files with 77 additions and 18 deletions.
11 changes: 11 additions & 0 deletions src/VSPoll.API/ConfigurationMapper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using AutoMapper;
using Entity = VSPoll.API.Persistence.Entity;

namespace VSPoll.API
{
public class ConfigurationMapper : Profile
{
public ConfigurationMapper()
=> CreateMap<Models.User, Entity.User>();
}
}
18 changes: 13 additions & 5 deletions src/VSPoll.API/Migrations/V1__Create_table.sql
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
create table "user" (
"id" text primary key,
first_name text not null,
last_name text not null,
username text not null,
photo_url text not null
);

create table poll (
id integer generated always as identity primary key,
multi_vote boolean not null default false,
show_voters boolean not null default true,
allow_add boolean not null default false,
end_date timestamp without time zone not null default now() + '7 days',
"user" text
user_id text references "user" on update cascade on delete cascade
);

create table poll_block (
poll_id integer not null references poll on delete cascade on update cascade,
"user" text not null,
constraint pk_poll_block primary key (poll_id, "user")
user_id text not null references "user" on delete cascade on update cascade,
constraint pk_poll_block primary key (poll_id, user_id)
);

create table poll_option (
Expand All @@ -22,6 +30,6 @@ create table poll_option (

create table poll_vote (
option_id integer not null references poll_option on delete cascade on update cascade,
"user" text not null,
constraint pk_poll_vote primary key (option_id, "user")
user_id text not null references "user" on delete cascade on update cascade,
constraint pk_poll_vote primary key (option_id, user_id)
);
2 changes: 1 addition & 1 deletion src/VSPoll.API/Models/Poll.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class Poll

public DateTime EndDate { get; set; }

public string? User { get; set; }
public User? User { get; set; }

public UserType UserType { get; set; }

Expand Down
8 changes: 7 additions & 1 deletion src/VSPoll.API/Models/User.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ public class User
{
public string Id { get; set; } = null!;

//ToDo: avatar, url to profile...
public string FirstName { get; set; } = null!;

public string LastName { get; set; } = null!;

public string Username { get; set; } = null!;

public string PhotoUrl { get; set; } = null!;
}
}
1 change: 1 addition & 0 deletions src/VSPoll.API/Persistence/Context/PollContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public class PollContext : DbContext
public DbSet<PollBlock> PollBlocks { get; set; } = null!;
public DbSet<PollOption> PollOptions { get; set; } = null!;
public DbSet<PollVote> PollVotes { get; set; } = null!;
public DbSet<User> Users { get; set; } = null!;

protected override void OnModelCreating(ModelBuilder modelBuilder)
=> modelBuilder.UseIdentityAlwaysColumns();
Expand Down
3 changes: 2 additions & 1 deletion src/VSPoll.API/Persistence/Entity/Poll.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ public class Poll

public DateTime EndDate { get; set; } = DateTime.Now.AddDays(7);

public string? User { get; set; }
public string? UserId { get; set; }
public User? User { get; set; }

public List<PollBlock> Blocks { get; set; } = new List<PollBlock>();
public List<PollOption> Options { get; set; } = new List<PollOption>();
Expand Down
3 changes: 2 additions & 1 deletion src/VSPoll.API/Persistence/Entity/PollBlock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ public class PollBlock
public int PollId { get; set; }
public Poll Poll { get; set; } = null!;

public string User { get; set; } = null!;
public string UserId { get; set; } = null!;
public User User { get; set; } = null!;
}
}
3 changes: 2 additions & 1 deletion src/VSPoll.API/Persistence/Entity/PollVote.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ public class PollVote
public int OptionId { get; set; }
public PollOption Option { get; set; } = null!;

public string User { get; set; } = null!;
public string UserId { get; set; } = null!;
public User User { get; set; } = null!;
}
}
15 changes: 15 additions & 0 deletions src/VSPoll.API/Persistence/Entity/User.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace VSPoll.API.Persistence.Entity
{
public class User
{
public string Id { get; set; } = null!;

public string FirstName { get; set; } = null!;

public string LastName { get; set; } = null!;

public string Username { get; set; } = null!;

public string PhotoUrl { get; set; } = null!;
}
}
16 changes: 9 additions & 7 deletions src/VSPoll.API/Services/PollService.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Linq;
using System.Threading.Tasks;
using AutoMapper;
using VSPoll.API.Models;
using VSPoll.API.Persistence.Repository;

Expand All @@ -8,8 +9,12 @@ namespace VSPoll.API.Services
public class PollService : IPollService
{
private readonly IPollRepository pollRepository;
public PollService(IPollRepository pollRepository)
=> this.pollRepository = pollRepository;
private readonly IMapper mapper;
public PollService(IPollRepository pollRepository, IMapper mapper)
{
this.pollRepository = pollRepository;
this.mapper = mapper;
}

public async Task<Poll> GetPollAsync(int id)
{
Expand All @@ -24,14 +29,11 @@ public async Task<Poll> GetPollAsync(int id)
Description = option.Description,
Id = option.Id,
Percentage = option.Votes.Count / poll.Options.Sum(opt => opt.Votes.Count),
Voters = option.Votes.Select(vote => new User
{
Id = vote.User,
}),
Voters = option.Votes.Select(vote => mapper.Map<User>(vote.User)),
Votes = option.Votes.Count,
}),
ShowVoters = poll.ShowVoters,
User = poll.User,
User = mapper.Map<User>(poll.User),
UserType = UserType.Visitor, //ToDo
};
}
Expand Down
14 changes: 13 additions & 1 deletion src/VSPoll.API/Startup.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.AspNetCore.Builder;
using AutoMapper;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
Expand All @@ -25,6 +26,17 @@ public void ConfigureServices(IServiceCollection services)
services.AddControllers();
SetupDatabase(services);
ConfigureSwagger(services);
AddSingletons(services);
}

private void AddSingletons(IServiceCollection services)
{
var mapping = new MapperConfiguration(x =>
{
x.AddProfile(new ConfigurationMapper());
});
var mapper = mapping.CreateMapper();
services.AddSingleton(mapper);
}

protected virtual void SetupDatabase(IServiceCollection services)
Expand Down
1 change: 1 addition & 0 deletions src/VSPoll.API/VSPoll.API.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="AutoMapper" Version="9.0.0" />
<PackageReference Include="Evolve" Version="2.4.0" />
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.10.8" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="3.1.3" />
Expand Down

0 comments on commit 14f936a

Please sign in to comment.