Skip to content

Commit 38c4af3

Browse files
authored
Merge pull request #233 from ITU-BDSA2024-GROUP10/modify-the-identity-register-page
Modify the identity register page
2 parents ebb2cd6 + 55453bf commit 38c4af3

23 files changed

+1277
-116
lines changed

Chirp/src/Chirp.Infrastructure/AuthorRepository.cs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public class AuthorRepository(ChirpDBContext context) : IAuthorRepository
1212
public async Task<AuthorDTO?> GetAuthorByName(string name) =>
1313
await context.Authors
1414
.Where(a => a.Name == name)
15-
.Select(a => new AuthorDTO(a.Name, a.Email))
15+
.Select(a => new AuthorDTO(a.Name, a.Email!))
1616
.FirstOrDefaultAsync();
1717

1818
public async Task<AuthorDTO?> GetAuthorByEmail(string email)
@@ -24,20 +24,14 @@ await context.Authors
2424

2525
var authors = await query.ToListAsync();
2626

27-
return authors.Select(author => new AuthorDTO(author.Name, author.Email)).FirstOrDefault();
27+
return authors.Select(author => new AuthorDTO(author.Name, author.Email ?? string.Empty)).FirstOrDefault();
2828
}
2929

3030
public async Task<bool> AddAuthor(AuthorDTO author)
3131
{
32-
var newAuthor = new Author
33-
{
34-
Name = author.Name,
35-
Email = author.Email
36-
};
37-
3832
try
3933
{
40-
context.Authors.Add(newAuthor);
34+
context.Authors.Add(Author.CreateAuthor(author));
4135
await context.SaveChangesAsync();
4236
return true;
4337
}

Chirp/src/Chirp.Infrastructure/ChirpDbContext.cs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace Chirp.Infrastructure;
66

7-
public class ChirpDBContext(DbContextOptions<ChirpDBContext> options) : IdentityDbContext<ApplicationUser>(options)
7+
public class ChirpDBContext(DbContextOptions<ChirpDBContext> options) : IdentityDbContext<Author>(options)
88
{
99
public DbSet<Cheep> Cheeps { get; set; }
1010
public DbSet<Author> Authors { get; set; }
@@ -38,22 +38,17 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
3838
//define author entity constraints
3939
modelBuilder.Entity<Author>(entity =>
4040
{
41-
//define auto increment key
42-
entity.HasKey(a => a.Id);
43-
entity.Property(a => a.Id)
44-
.ValueGeneratedOnAdd();
45-
4641
//define required + unique name
4742
entity.Property(a => a.Name)
4843
.IsRequired();
49-
entity.HasIndex(a => a.Name)
50-
.IsUnique();
51-
52-
//define required + unique email
5344
entity.Property(a => a.Email)
5445
.IsRequired();
46+
entity.HasIndex(a => a.Name)
47+
.IsUnique();
5548
entity.HasIndex(a => a.Email)
5649
.IsUnique();
5750
});
51+
52+
5853
}
5954
}

Chirp/src/Chirp.Infrastructure/DbInitializer.cs

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,40 @@
1+
using System.Security.Claims;
12
using Chirp.Infrastructure.Model;
3+
using Microsoft.AspNetCore.Identity;
4+
using Microsoft.Extensions.DependencyInjection;
25

36
namespace Chirp.Infrastructure;
47

58
public static class DbInitializer
69
{
7-
public static void SeedDatabase(ChirpDBContext chirpContext)
10+
public static async void SeedDatabase(ChirpDBContext chirpContext, IServiceProvider serviceProvider)
811
{
912
if (!(chirpContext.Authors.Any() && chirpContext.Cheeps.Any()))
1013
{
11-
var a1 = new Author() { Id = 1, Name = "Roger Histand", Email = "Roger+Histand@hotmail.com", Cheeps = new List<Cheep>() };
12-
var a2 = new Author() { Id = 2, Name = "Luanna Muro", Email = "Luanna-Muro@ku.dk", Cheeps = new List<Cheep>() };
13-
var a3 = new Author() { Id = 3, Name = "Wendell Ballan", Email = "Wendell-Ballan@gmail.com", Cheeps = new List<Cheep>() };
14-
var a4 = new Author() { Id = 4, Name = "Nathan Sirmon", Email = "Nathan+Sirmon@dtu.dk", Cheeps = new List<Cheep>() };
15-
var a5 = new Author() { Id = 5, Name = "Quintin Sitts", Email = "Quintin+Sitts@itu.dk", Cheeps = new List<Cheep>() };
16-
var a6 = new Author() { Id = 6, Name = "Mellie Yost", Email = "Mellie+Yost@ku.dk", Cheeps = new List<Cheep>() };
17-
var a7 = new Author() { Id = 7, Name = "Malcolm Janski", Email = "Malcolm-Janski@gmail.com", Cheeps = new List<Cheep>() };
18-
var a8 = new Author() { Id = 8, Name = "Octavio Wagganer", Email = "Octavio.Wagganer@dtu.dk", Cheeps = new List<Cheep>() };
19-
var a9 = new Author() { Id = 9, Name = "Johnnie Calixto", Email = "Johnnie+Calixto@itu.dk", Cheeps = new List<Cheep>() };
20-
var a10 = new Author() { Id = 10, Name = "Jacqualine Gilcoine", Email = "Jacqualine.Gilcoine@gmail.com", Cheeps = new List<Cheep>() };
21-
var a11 = new Author() { Id = 11, Name = "Helge", Email = "ropf@itu.dk", Cheeps = new List<Cheep>() };
22-
var a12 = new Author() { Id = 12, Name = "Adrian", Email = "adho@itu.dk", Cheeps = new List<Cheep>() };
14+
var a1 = new Author() { Id = "1", Name = "Roger Histand", Email = "Roger+Histand@hotmail.com", Cheeps = new List<Cheep>() };
15+
var a2 = new Author() { Id = "2", Name = "Luanna Muro", Email = "Luanna-Muro@ku.dk", Cheeps = new List<Cheep>() };
16+
var a3 = new Author() { Id = "3", Name = "Wendell Ballan", Email = "Wendell-Ballan@gmail.com", Cheeps = new List<Cheep>() };
17+
var a4 = new Author() { Id = "4", Name = "Nathan Sirmon", Email = "Nathan+Sirmon@dtu.dk", Cheeps = new List<Cheep>() };
18+
var a5 = new Author() { Id = "5", Name = "Quintin Sitts", Email = "Quintin+Sitts@itu.dk", Cheeps = new List<Cheep>() };
19+
var a6 = new Author() { Id = "6", Name = "Mellie Yost", Email = "Mellie+Yost@ku.dk", Cheeps = new List<Cheep>() };
20+
var a7 = new Author() { Id = "7", Name = "Malcolm Janski", Email = "Malcolm-Janski@gmail.com", Cheeps = new List<Cheep>() };
21+
var a8 = new Author() { Id = "8", Name = "Octavio Wagganer", Email = "Octavio.Wagganer@dtu.dk", Cheeps = new List<Cheep>() };
22+
var a9 = new Author() { Id = "9", Name = "Johnnie Calixto", Email = "Johnnie+Calixto@itu.dk", Cheeps = new List<Cheep>() };
23+
var a10 = new Author() { Id = "10", Name = "Jacqualine Gilcoine", Email = "Jacqualine.Gilcoine@gmail.com", Cheeps = new List<Cheep>() };
2324

24-
var authors = new List<Author>() { a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12 };
25+
var userManager = serviceProvider.GetRequiredService<UserManager<Author>>();
26+
var a11 = new Author {UserName = "ropf@itu.dk", Email = "ropf@itu.dk", Cheeps = new List<Cheep>(), Name = "Helge"};
27+
var a12 = new Author {UserName = "adho@itu.dk", Email = "adho@itu.dk", Cheeps = new List<Cheep>(), Name = "Adrian"};
28+
a11.EmailConfirmed = true;
29+
a12.EmailConfirmed = true;
30+
await userManager.CreateAsync(a11, "LetM31n!");
31+
await userManager.CreateAsync(a12, "M32Want_Access");
32+
33+
var claim11 = new Claim("UserName", a11.Name);
34+
var claim12 = new Claim("UserName", a12.Name);
35+
await userManager.AddClaimAsync(a11, claim11);
36+
await userManager.AddClaimAsync(a12, claim12);
37+
var authors = new List<Author>() { a1, a2, a3, a4, a5, a6, a7, a8, a9, a10};
2538

2639
var c1 = new Cheep() { Id = 1, Author = a10, Message = "They were married in Chicago, with old Smith, and was expected aboard every day; meantime, the two went past me.", TimeStamp = DateTime.Parse("2023-08-01 13:14:37") };
2740
var c2 = new Cheep() { Id = 2, Author = a10, Message = "And then, as he listened to all that''s left o'' twenty-one people.", TimeStamp = DateTime.Parse("2023-08-01 13:15:21") };

0 commit comments

Comments
 (0)