Skip to content

Commit 1a8d0c4

Browse files
author
Daniel Mititelu
committed
Add todoitem service and fix authentification
1 parent 276c799 commit 1a8d0c4

20 files changed

+335
-102
lines changed

TodoApp.DTO/TodoItemDto.cs

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace TodoApp.DTO
2+
{
3+
public class TodoItemDto
4+
{
5+
public int UserId { get; set; }
6+
public string Description { get; set; }
7+
}
8+
}

TodoApp.DataAccess/AppDbContext.cs

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ namespace TodoApp.DataAccess
66
public class AppDbContext: DbContext
77
{
88
public virtual DbSet<User> User { get; set; }
9+
public virtual DbSet<TodoItem> TodoItem { get; set; }
910

1011
public AppDbContext(DbContextOptions<AppDbContext> options)
1112
: base(options) { }

TodoApp.DataAccess/Migrations/20181010180651_AddTodoItem.Designer.cs

+65
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
using Microsoft.EntityFrameworkCore.Metadata;
2+
using Microsoft.EntityFrameworkCore.Migrations;
3+
4+
namespace TodoApp.DataAccess.Migrations
5+
{
6+
public partial class AddTodoItem : Migration
7+
{
8+
protected override void Up(MigrationBuilder migrationBuilder)
9+
{
10+
migrationBuilder.CreateTable(
11+
name: "TodoItem",
12+
columns: table => new
13+
{
14+
TodoItemId = table.Column<int>(nullable: false)
15+
.Annotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn),
16+
Description = table.Column<string>(nullable: true),
17+
UserId = table.Column<int>(nullable: true)
18+
},
19+
constraints: table =>
20+
{
21+
table.PrimaryKey("PK_TodoItem", x => x.TodoItemId);
22+
table.ForeignKey(
23+
name: "FK_TodoItem_User_UserId",
24+
column: x => x.UserId,
25+
principalTable: "User",
26+
principalColumn: "UserId",
27+
onDelete: ReferentialAction.Restrict);
28+
});
29+
30+
migrationBuilder.CreateIndex(
31+
name: "IX_TodoItem_UserId",
32+
table: "TodoItem",
33+
column: "UserId");
34+
}
35+
36+
protected override void Down(MigrationBuilder migrationBuilder)
37+
{
38+
migrationBuilder.DropTable(
39+
name: "TodoItem");
40+
}
41+
}
42+
}

TodoApp.DataAccess/Migrations/AppDbContextModelSnapshot.cs

+26-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// <auto-generated />
2+
using System;
23
using Microsoft.EntityFrameworkCore;
34
using Microsoft.EntityFrameworkCore.Infrastructure;
45
using Microsoft.EntityFrameworkCore.Metadata;
@@ -14,10 +15,27 @@ protected override void BuildModel(ModelBuilder modelBuilder)
1415
{
1516
#pragma warning disable 612, 618
1617
modelBuilder
17-
.HasAnnotation("ProductVersion", "2.1.1-rtm-30846")
18+
.HasAnnotation("ProductVersion", "2.1.3-rtm-32065")
1819
.HasAnnotation("Relational:MaxIdentifierLength", 128)
1920
.HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);
2021

22+
modelBuilder.Entity("TodoApp.Entity.TodoItem", b =>
23+
{
24+
b.Property<int>("TodoItemId")
25+
.ValueGeneratedOnAdd()
26+
.HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);
27+
28+
b.Property<string>("Description");
29+
30+
b.Property<int?>("UserId");
31+
32+
b.HasKey("TodoItemId");
33+
34+
b.HasIndex("UserId");
35+
36+
b.ToTable("TodoItem");
37+
});
38+
2139
modelBuilder.Entity("TodoApp.Entity.User", b =>
2240
{
2341
b.Property<int>("UserId")
@@ -32,6 +50,13 @@ protected override void BuildModel(ModelBuilder modelBuilder)
3250

3351
b.ToTable("User");
3452
});
53+
54+
modelBuilder.Entity("TodoApp.Entity.TodoItem", b =>
55+
{
56+
b.HasOne("TodoApp.Entity.User", "User")
57+
.WithMany("TodoItems")
58+
.HasForeignKey("UserId");
59+
});
3560
#pragma warning restore 612, 618
3661
}
3762
}

TodoApp.Entity/TodoItem.cs

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace TodoApp.Entity
2+
{
3+
public class TodoItem
4+
{
5+
public int TodoItemId { get; set; }
6+
public string Description { get; set; }
7+
public User User { get; set; }
8+
}
9+
}

TodoApp.Entity/User.cs

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Text;
1+
using System.Collections.Generic;
42

53
namespace TodoApp.Entity
64
{
@@ -9,5 +7,6 @@ public class User
97
public int UserId { get; set; }
108
public string Email { get; set; }
119
public string EncryptedPassword { get; set; }
10+
public IList<TodoItem> TodoItems { get; set; }
1211
}
1312
}

TodoApp.Services/Authentification/AuthService.cs

+9-4
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,21 @@ public AuthService(AppDbContext dbContext)
1818

1919
public bool AreCredentialsValid(UserDto userDto)
2020
{
21-
var user = Mapper.Map<UserDto, User>(userDto);
21+
var encryptedPassword = CryptographyUtils.EncryptPassword(userDto.Password);
2222
return (from u in _dbContext.User
23-
where string.Equals(u.Email, user.Email, StringComparison.CurrentCultureIgnoreCase)
24-
&& u.EncryptedPassword == user.EncryptedPassword
23+
where string.Equals(u.Email, userDto.Email, StringComparison.CurrentCultureIgnoreCase)
24+
&& u.EncryptedPassword == encryptedPassword
2525
select true).FirstOrDefault();
2626
}
2727

2828
public void Register(UserDto userDto)
2929
{
30-
var user = Mapper.Map<UserDto, User>(userDto);
30+
var user = new User
31+
{
32+
Email = userDto.Email,
33+
EncryptedPassword = CryptographyUtils.EncryptPassword(userDto.Password),
34+
};
35+
3136
_dbContext.User.Add(user);
3237
_dbContext.SaveChanges();
3338
}

TodoApp.Services/Authentification/MappingService.cs

-27
This file was deleted.

TodoApp.Services/MappingService.cs

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using AutoMapper;
2+
using TodoApp.DTO;
3+
using TodoApp.Entity;
4+
5+
namespace TodoApp.Services
6+
{
7+
public static class MappingService
8+
{
9+
public static void Initialize()
10+
{
11+
Mapper.Initialize(cfg => cfg.CreateMap<TodoItem, TodoItemDto>().ConvertUsing<TodoItemConvertor>());
12+
}
13+
}
14+
15+
public class TodoItemConvertor : ITypeConverter<TodoItem, TodoItemDto>
16+
{
17+
public TodoItemDto Convert(TodoItem source, TodoItemDto destination, ResolutionContext context)
18+
{
19+
return new TodoItemDto
20+
{
21+
Description = source.Description
22+
};
23+
}
24+
}
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
using AutoMapper;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using TodoApp.DataAccess;
5+
using TodoApp.DTO;
6+
using TodoApp.Entity;
7+
8+
namespace TodoApp.Services
9+
{
10+
public class TodoItemService
11+
{
12+
private readonly AppDbContext _dbContext;
13+
14+
public TodoItemService(AppDbContext dbContext)
15+
{
16+
_dbContext = dbContext;
17+
}
18+
19+
public IEnumerable<TodoItemDto> Get(string email)
20+
{
21+
return _dbContext.TodoItem.Where(t => t.User.Email == email)
22+
.Select(t => Mapper.Map<TodoItem, TodoItemDto>(t)).ToList();
23+
}
24+
25+
public void Add(TodoItemDto todoItemDto, UserDto userDto)
26+
{
27+
var user = GetUser(userDto);
28+
_dbContext.Add(new TodoItem
29+
{
30+
Description = todoItemDto.Description,
31+
User = user
32+
});
33+
_dbContext.SaveChanges();
34+
}
35+
36+
public void Remove(TodoItemDto todoItemDto, UserDto userDto)
37+
{
38+
var user = GetUser(userDto);
39+
_dbContext.Remove(new TodoItem
40+
{
41+
Description = todoItemDto.Description,
42+
User = user
43+
});
44+
_dbContext.SaveChanges();
45+
}
46+
47+
private User GetUser(UserDto userDto)
48+
{
49+
return _dbContext.User.First(u => u.Email == userDto.Email);
50+
}
51+
}
52+
}

TodoApp/ClientApp/src/app/app.module.ts

+2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { RegisterComponent } from './register/register.component';
1414
import { AuthenticationService } from './services/authentication.service';
1515
import { JwtInterceptor } from './helpers/jwt.interceptor';
1616
import { AuthGuard } from './guards/auth.guard';
17+
import { TodoItemService } from "./services/todoitem.service";
1718

1819
@
1920
NgModule({
@@ -47,6 +48,7 @@ NgModule({
4748
providers: [
4849
AuthGuard,
4950
AuthenticationService,
51+
TodoItemService,
5052
{ provide: HTTP_INTERCEPTORS, useClass: JwtInterceptor, multi: true }
5153
],
5254
entryComponents: [LogoutComponent],
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,22 @@
1-
import { Component } from '@angular/core';
1+
import { Component, AfterViewInit } from '@angular/core';
2+
import { TodoItemService } from "../services/todoitem.service";
23

34
@Component({
45
selector: 'app-home-layout',
56
templateUrl: './home-layout.component.html',
67
})
7-
export class HomeLayoutComponent {
8+
export class HomeLayoutComponent implements AfterViewInit {
9+
10+
constructor(private readonly todoItemService: TodoItemService) {
11+
12+
}
13+
14+
ngAfterViewInit(): void {
15+
this.todoItemService.get().
16+
subscribe(response => {
17+
debugger;
18+
console.log(response);
19+
});
20+
}
21+
822
}

TodoApp/ClientApp/src/app/services/authentication.service.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import { Injectable } from '@angular/core';
2-
import { Http } from '@angular/http';
2+
import { HttpClient } from '@angular/common/http';
33
import { Observable } from 'rxjs';
44
import { catchError, map } from 'rxjs/operators';
55

66
@Injectable()
77
export class AuthenticationService {
88

9-
constructor(private readonly http: Http) { }
9+
constructor(private readonly http: HttpClient) { }
1010

1111
register(body: User) {
1212
return this.http.post('/api/auth/register', body)

0 commit comments

Comments
 (0)