Commit f05ca624 authored by boncho vylkov's avatar boncho vylkov

add new branch with cut basic version

parent 29c03843
namespace MvcCoreTemplate.Data.Models
{
using System.ComponentModel.DataAnnotations;
using MvcCoreTemplate.Data.Common.Models;
public class TodoItem : BaseDeletableModel<int>
{
[Required]
public string Title { get; set; }
public bool IsDone { get; set; }
[Required]
public string AuthorId { get; set; }
public virtual ApplicationUser Author { get; set; }
}
}
......@@ -24,7 +24,7 @@
{
}
public DbSet<TodoItem> TodoItems { get; set; }
public override int SaveChanges() => this.SaveChanges(true);
......
// <auto-generated />
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage;
using Microsoft.EntityFrameworkCore.Storage.Internal;
using MvcCoreTemplate.Data;
using System;
namespace MvcCoreTemplate.Data.Migrations
{
[DbContext(typeof(ApplicationDbContext))]
[Migration("20180426151235_RemoveToDoTable")]
partial class RemoveToDoTable
{
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "2.0.1-rtm-125")
.HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);
modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityRoleClaim<string>", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd();
b.Property<string>("ClaimType");
b.Property<string>("ClaimValue");
b.Property<string>("RoleId")
.IsRequired();
b.HasKey("Id");
b.HasIndex("RoleId");
b.ToTable("AspNetRoleClaims");
});
modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserClaim<string>", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd();
b.Property<string>("ClaimType");
b.Property<string>("ClaimValue");
b.Property<string>("UserId")
.IsRequired();
b.HasKey("Id");
b.HasIndex("UserId");
b.ToTable("AspNetUserClaims");
});
modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserLogin<string>", b =>
{
b.Property<string>("LoginProvider");
b.Property<string>("ProviderKey");
b.Property<string>("ProviderDisplayName");
b.Property<string>("UserId")
.IsRequired();
b.HasKey("LoginProvider", "ProviderKey");
b.HasIndex("UserId");
b.ToTable("AspNetUserLogins");
});
modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserRole<string>", b =>
{
b.Property<string>("UserId");
b.Property<string>("RoleId");
b.HasKey("UserId", "RoleId");
b.HasIndex("RoleId");
b.ToTable("AspNetUserRoles");
});
modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserToken<string>", b =>
{
b.Property<string>("UserId");
b.Property<string>("LoginProvider");
b.Property<string>("Name");
b.Property<string>("Value");
b.HasKey("UserId", "LoginProvider", "Name");
b.ToTable("AspNetUserTokens");
});
modelBuilder.Entity("MvcCoreTemplate.Data.Models.ApplicationRole", b =>
{
b.Property<string>("Id")
.ValueGeneratedOnAdd();
b.Property<string>("ConcurrencyStamp")
.IsConcurrencyToken();
b.Property<DateTime>("CreatedOn");
b.Property<DateTime?>("DeletedOn");
b.Property<bool>("IsDeleted");
b.Property<DateTime?>("ModifiedOn");
b.Property<string>("Name")
.HasMaxLength(256);
b.Property<string>("NormalizedName")
.HasMaxLength(256);
b.HasKey("Id");
b.HasIndex("IsDeleted");
b.HasIndex("NormalizedName")
.IsUnique()
.HasName("RoleNameIndex")
.HasFilter("[NormalizedName] IS NOT NULL");
b.ToTable("AspNetRoles");
});
modelBuilder.Entity("MvcCoreTemplate.Data.Models.ApplicationUser", b =>
{
b.Property<string>("Id")
.ValueGeneratedOnAdd();
b.Property<int>("AccessFailedCount");
b.Property<string>("ConcurrencyStamp")
.IsConcurrencyToken();
b.Property<DateTime>("CreatedOn");
b.Property<DateTime?>("DeletedOn");
b.Property<string>("Email")
.HasMaxLength(256);
b.Property<bool>("EmailConfirmed");
b.Property<bool>("IsDeleted");
b.Property<bool>("LockoutEnabled");
b.Property<DateTimeOffset?>("LockoutEnd");
b.Property<DateTime?>("ModifiedOn");
b.Property<string>("NormalizedEmail")
.HasMaxLength(256);
b.Property<string>("NormalizedUserName")
.HasMaxLength(256);
b.Property<string>("PasswordHash");
b.Property<string>("PhoneNumber");
b.Property<bool>("PhoneNumberConfirmed");
b.Property<string>("SecurityStamp");
b.Property<bool>("TwoFactorEnabled");
b.Property<string>("UserName")
.HasMaxLength(256);
b.HasKey("Id");
b.HasIndex("IsDeleted");
b.HasIndex("NormalizedEmail")
.HasName("EmailIndex");
b.HasIndex("NormalizedUserName")
.IsUnique()
.HasName("UserNameIndex")
.HasFilter("[NormalizedUserName] IS NOT NULL");
b.ToTable("AspNetUsers");
});
modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityRoleClaim<string>", b =>
{
b.HasOne("MvcCoreTemplate.Data.Models.ApplicationRole")
.WithMany()
.HasForeignKey("RoleId")
.OnDelete(DeleteBehavior.Restrict);
});
modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserClaim<string>", b =>
{
b.HasOne("MvcCoreTemplate.Data.Models.ApplicationUser")
.WithMany("Claims")
.HasForeignKey("UserId")
.OnDelete(DeleteBehavior.Restrict);
});
modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserLogin<string>", b =>
{
b.HasOne("MvcCoreTemplate.Data.Models.ApplicationUser")
.WithMany("Logins")
.HasForeignKey("UserId")
.OnDelete(DeleteBehavior.Restrict);
});
modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserRole<string>", b =>
{
b.HasOne("MvcCoreTemplate.Data.Models.ApplicationRole")
.WithMany()
.HasForeignKey("RoleId")
.OnDelete(DeleteBehavior.Restrict);
b.HasOne("MvcCoreTemplate.Data.Models.ApplicationUser")
.WithMany("Roles")
.HasForeignKey("UserId")
.OnDelete(DeleteBehavior.Restrict);
});
modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserToken<string>", b =>
{
b.HasOne("MvcCoreTemplate.Data.Models.ApplicationUser")
.WithMany()
.HasForeignKey("UserId")
.OnDelete(DeleteBehavior.Restrict);
});
#pragma warning restore 612, 618
}
}
}
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Migrations;
using System;
using System.Collections.Generic;
namespace MvcCoreTemplate.Data.Migrations
{
public partial class RemoveToDoTable : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "TodoItems");
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "TodoItems",
columns: table => new
{
Id = table.Column<int>(nullable: false)
.Annotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn),
AuthorId = table.Column<string>(nullable: false),
CreatedOn = table.Column<DateTime>(nullable: false),
DeletedOn = table.Column<DateTime>(nullable: true),
IsDeleted = table.Column<bool>(nullable: false),
IsDone = table.Column<bool>(nullable: false),
ModifiedOn = table.Column<DateTime>(nullable: true),
Title = table.Column<string>(nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_TodoItems", x => x.Id);
table.ForeignKey(
name: "FK_TodoItems_AspNetUsers_AuthorId",
column: x => x.AuthorId,
principalTable: "AspNetUsers",
principalColumn: "Id",
onDelete: ReferentialAction.Restrict);
});
migrationBuilder.CreateIndex(
name: "IX_TodoItems_AuthorId",
table: "TodoItems",
column: "AuthorId");
migrationBuilder.CreateIndex(
name: "IX_TodoItems_IsDeleted",
table: "TodoItems",
column: "IsDeleted");
}
}
}
// <auto-generated />
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage;
using Microsoft.EntityFrameworkCore.Storage.Internal;
using MvcCoreTemplate.Data;
using System;
namespace MvcCoreTemplate.Data.Migrations
{
using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
[DbContext(typeof(ApplicationDbContext))]
public partial class ApplicationDbContextModelSnapshot : ModelSnapshot
partial class ApplicationDbContextModelSnapshot : ModelSnapshot
{
protected override void BuildModel(ModelBuilder modelBuilder)
{
......@@ -17,6 +20,90 @@ namespace MvcCoreTemplate.Data.Migrations
.HasAnnotation("ProductVersion", "2.0.1-rtm-125")
.HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);
modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityRoleClaim<string>", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd();
b.Property<string>("ClaimType");
b.Property<string>("ClaimValue");
b.Property<string>("RoleId")
.IsRequired();
b.HasKey("Id");
b.HasIndex("RoleId");
b.ToTable("AspNetRoleClaims");
});
modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserClaim<string>", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd();
b.Property<string>("ClaimType");
b.Property<string>("ClaimValue");
b.Property<string>("UserId")
.IsRequired();
b.HasKey("Id");
b.HasIndex("UserId");
b.ToTable("AspNetUserClaims");
});
modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserLogin<string>", b =>
{
b.Property<string>("LoginProvider");
b.Property<string>("ProviderKey");
b.Property<string>("ProviderDisplayName");
b.Property<string>("UserId")
.IsRequired();
b.HasKey("LoginProvider", "ProviderKey");
b.HasIndex("UserId");
b.ToTable("AspNetUserLogins");
});
modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserRole<string>", b =>
{
b.Property<string>("UserId");
b.Property<string>("RoleId");
b.HasKey("UserId", "RoleId");
b.HasIndex("RoleId");
b.ToTable("AspNetUserRoles");
});
modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserToken<string>", b =>
{
b.Property<string>("UserId");
b.Property<string>("LoginProvider");
b.Property<string>("Name");
b.Property<string>("Value");
b.HasKey("UserId", "LoginProvider", "Name");
b.ToTable("AspNetUserTokens");
});
modelBuilder.Entity("MvcCoreTemplate.Data.Models.ApplicationRole", b =>
{
b.Property<string>("Id")
......@@ -112,128 +199,6 @@ namespace MvcCoreTemplate.Data.Migrations
b.ToTable("AspNetUsers");
});
modelBuilder.Entity("MvcCoreTemplate.Data.Models.TodoItem", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd();
b.Property<string>("AuthorId")
.IsRequired();
b.Property<DateTime>("CreatedOn");
b.Property<DateTime?>("DeletedOn");
b.Property<bool>("IsDeleted");
b.Property<bool>("IsDone");
b.Property<DateTime?>("ModifiedOn");
b.Property<string>("Title")
.IsRequired();
b.HasKey("Id");
b.HasIndex("AuthorId");
b.HasIndex("IsDeleted");
b.ToTable("TodoItems");
});
modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityRoleClaim<string>", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd();
b.Property<string>("ClaimType");
b.Property<string>("ClaimValue");
b.Property<string>("RoleId")
.IsRequired();
b.HasKey("Id");
b.HasIndex("RoleId");
b.ToTable("AspNetRoleClaims");
});
modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserClaim<string>", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd();
b.Property<string>("ClaimType");
b.Property<string>("ClaimValue");
b.Property<string>("UserId")
.IsRequired();
b.HasKey("Id");
b.HasIndex("UserId");
b.ToTable("AspNetUserClaims");
});
modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserLogin<string>", b =>
{
b.Property<string>("LoginProvider");
b.Property<string>("ProviderKey");
b.Property<string>("ProviderDisplayName");
b.Property<string>("UserId")
.IsRequired();
b.HasKey("LoginProvider", "ProviderKey");
b.HasIndex("UserId");
b.ToTable("AspNetUserLogins");
});
modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserRole<string>", b =>
{
b.Property<string>("UserId");
b.Property<string>("RoleId");
b.HasKey("UserId", "RoleId");
b.HasIndex("RoleId");
b.ToTable("AspNetUserRoles");
});
modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserToken<string>", b =>
{
b.Property<string>("UserId");
b.Property<string>("LoginProvider");
b.Property<string>("Name");
b.Property<string>("Value");
b.HasKey("UserId", "LoginProvider", "Name");
b.ToTable("AspNetUserTokens");
});
modelBuilder.Entity("MvcCoreTemplate.Data.Models.TodoItem", b =>
{
b.HasOne("MvcCoreTemplate.Data.Models.ApplicationUser", "Author")
.WithMany()
.HasForeignKey("AuthorId")
.OnDelete(DeleteBehavior.Restrict);
});
modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityRoleClaim<string>", b =>
{
b.HasOne("MvcCoreTemplate.Data.Models.ApplicationRole")
......
......@@ -39,8 +39,4 @@
<ProjectReference Include="..\MvcCoreTemplate.Data.Models\MvcCoreTemplate.Data.Models.csproj" />
</ItemGroup>
<ItemGroup>
<Folder Include="Migrations\" />
</ItemGroup>
</Project>
\ No newline at end of file
namespace MvcCoreTemplate.Web.Controllers
{
using System.Linq;
using System.Threading.Tasks;
using MvcCoreTemplate.Common.Mapping;
using MvcCoreTemplate.Data.Common.Repositories;
using MvcCoreTemplate.Data.Models;
using MvcCoreTemplate.Web.Infrastructure.Extensions;
using MvcCoreTemplate.Web.ViewModels.TodoItems;
using AutoMapper;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Mvc;
public class TodoItemsController : BaseController
{
private readonly IDeletableEntityRepository<TodoItem> repository;
public TodoItemsController(IDeletableEntityRepository<TodoItem> repository)
{
this.repository = repository;
}
[HttpGet]
public IActionResult All()
{
var userId = this.User.GetId();
var todoItems = this.repository.All().Where(t => t.AuthorId == userId).To<TodoItemViewModel>().ToList();
return this.Ok(todoItems);
}
[HttpPost]
public async Task<IActionResult> Create([FromBody]TodoItemBindingModel model)
{
if (model == null || !this.ModelState.IsValid)
{
return this.BadRequest(this.ModelState.GetFirstError());
}
var todoItem = Mapper.Map<TodoItem>(model);
todoItem.AuthorId = this.User.GetId();
this.repository.Add(todoItem);
await this.repository.SaveChangesAsync();
return this.Ok(Mapper.Map<TodoItemViewModel>(todoItem));
}
[HttpPost]
public async Task<IActionResult> MarkAsDone(int id)
{
var todoItem = await this.repository.GetByIdAsync(id);
if (todoItem == null)
{
return this.NotFound();
}
if (todoItem.AuthorId != this.User.GetId())
{
return this.Forbid(JwtBearerDefaults.AuthenticationScheme);
}
if (!todoItem.IsDone)
{
todoItem.IsDone = true;
this.repository.Update(todoItem);
await this.repository.SaveChangesAsync();