-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
not using automapper anymore new post poll endpoint fixed a lot of stuff
- Loading branch information
Showing
18 changed files
with
167 additions
and
82 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
using Microsoft.Extensions.DependencyInjection; | ||
using VSPoll.API.Persistence.Repository; | ||
using VSPoll.API.Services; | ||
|
||
namespace VSPoll.API.Extensions | ||
{ | ||
public static class ServiceCollection | ||
{ | ||
public static IServiceCollection AddRepositories(this IServiceCollection services) | ||
{ | ||
services.AddTransient<IPollRepository, PollRepository>(); | ||
return services; | ||
} | ||
|
||
public static IServiceCollection AddServices(this IServiceCollection services) | ||
{ | ||
services.AddTransient<IPollService, PollService>(); | ||
return services; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,28 @@ | ||
create table "user" ( | ||
"id" text primary key, | ||
create table users ( | ||
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, | ||
create table polls ( | ||
id uuid 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' | ||
); | ||
|
||
create table poll_option ( | ||
id integer generated always as identity primary key, | ||
poll_id integer not null references poll on delete cascade on update cascade, | ||
create table poll_options ( | ||
id uuid primary key, | ||
poll_id uuid not null references polls on delete cascade on update cascade, | ||
description text not null, | ||
unique (poll_id, description) | ||
); | ||
|
||
create table poll_vote ( | ||
option_id integer not null references poll_option on delete cascade on update cascade, | ||
user_id text not null references "user" on delete cascade on update cascade, | ||
create table poll_votes ( | ||
option_id uuid not null references poll_options on delete cascade on update cascade, | ||
user_id text not null references users on delete cascade on update cascade, | ||
constraint pk_poll_vote primary key (option_id, user_id) | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
namespace VSPoll.API.Models | ||
{ | ||
public class PollCreate | ||
{ | ||
public bool MultiVote { get; set; } | ||
|
||
public bool ShowVoters { get; set; } | ||
|
||
public bool AllowAdd { get; set; } | ||
|
||
public DateTime EndDate { get; set; } | ||
|
||
public IEnumerable<string> Options { get; set; } = Enumerable.Empty<string>(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,12 @@ | ||
using System.Threading.Tasks; | ||
using System; | ||
using System.Threading.Tasks; | ||
using VSPoll.API.Persistence.Entity; | ||
|
||
namespace VSPoll.API.Persistence.Repository | ||
{ | ||
public interface IPollRepository | ||
{ | ||
Task<Entity.Poll> GetByIdAsync(int id); | ||
Task<Poll> GetByIdAsync(Guid id); | ||
Task InsertPollAsync(Poll poll); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,12 @@ | ||
using System.Threading.Tasks; | ||
using System; | ||
using System.Threading.Tasks; | ||
using VSPoll.API.Models; | ||
|
||
namespace VSPoll.API.Services | ||
{ | ||
public interface IPollService | ||
{ | ||
Task<Poll> GetPollAsync(int id); | ||
Task<Poll> GetPollAsync(Guid id); | ||
Task<Poll> InsertPollAsync(PollCreate pollCreate); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,28 @@ | ||
using System.Linq; | ||
using System; | ||
using System.Threading.Tasks; | ||
using AutoMapper; | ||
using VSPoll.API.Models; | ||
using VSPoll.API.Persistence.Repository; | ||
using Entity = VSPoll.API.Persistence.Entity; | ||
|
||
namespace VSPoll.API.Services | ||
{ | ||
public class PollService : IPollService | ||
{ | ||
private readonly IPollRepository pollRepository; | ||
private readonly IMapper mapper; | ||
public PollService(IPollRepository pollRepository, IMapper mapper) | ||
public PollService(IPollRepository pollRepository) | ||
=> this.pollRepository = pollRepository; | ||
|
||
public async Task<Poll> GetPollAsync(Guid id) | ||
{ | ||
this.pollRepository = pollRepository; | ||
this.mapper = mapper; | ||
var poll = await pollRepository.GetByIdAsync(id); | ||
return new Poll(poll); | ||
} | ||
|
||
public async Task<Poll> GetPollAsync(int id) | ||
public async Task<Poll> InsertPollAsync(PollCreate pollCreate) | ||
{ | ||
var poll = await pollRepository.GetByIdAsync(id); | ||
return new Poll | ||
{ | ||
AllowAdd = poll.AllowAdd, | ||
EndDate = poll.EndDate, | ||
MultiVote = poll.MultiVote, | ||
Options = poll.Options.Select(option => new PollOption | ||
{ | ||
Description = option.Description, | ||
Id = option.Id, | ||
Percentage = option.Votes.Count / poll.Options.Sum(opt => opt.Votes.Count), | ||
Voters = option.Votes.Select(vote => mapper.Map<User>(vote.User)), | ||
Votes = option.Votes.Count, | ||
}), | ||
ShowVoters = poll.ShowVoters, | ||
}; | ||
var poll = new Entity.Poll(pollCreate); | ||
await pollRepository.InsertPollAsync(poll); | ||
return new Poll(poll); | ||
} | ||
} | ||
} |
Oops, something went wrong.