Skip to content
Snippets Groups Projects
Commit 98b28b5f authored by esantangelo's avatar esantangelo
Browse files

add endpoint file/create

parent ae28a95d
No related branches found
No related tags found
3 merge requests!20merge from develop,!16Feature/chat signalr,!15Feature/upload file
Showing
with 1310 additions and 0 deletions
...@@ -5,3 +5,4 @@ ...@@ -5,3 +5,4 @@
.vs .vs
obj obj
bin bin
/Tsi1.Api/Tsi1.Api/wwwroot/fing/Calculo 1
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Tsi1.BusinessLayer.Helpers;
using Tsi1.BusinessLayer.Interfaces;
namespace Tsi1.Api.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class FileController : ControllerBase
{
private readonly IFileService _fileService;
public FileController(IFileService fileService)
{
_fileService = fileService;
}
[Authorize(Roles = UserTypes.Professor)]
[HttpPost("Create/{courseId}")]
public async Task<IActionResult> Create(IFormFile file, int courseId)
{
var tenantId = int.Parse(HttpContext.User.Claims.FirstOrDefault(x => x.Type == "TenantId").Value);
var result = await _fileService.Create(file, tenantId, courseId);
if (result.HasError)
{
return BadRequest(result.Message);
}
return Ok(result.Data);
}
}
}
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
...@@ -9,6 +10,7 @@ using Microsoft.AspNetCore.Builder; ...@@ -9,6 +10,7 @@ using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy; using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.StaticFiles;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
...@@ -71,6 +73,7 @@ namespace Tsi1.Api ...@@ -71,6 +73,7 @@ namespace Tsi1.Api
services.AddScoped<IPostService, PostService>(); services.AddScoped<IPostService, PostService>();
services.AddScoped<IPostMessageService, PostMessageService>(); services.AddScoped<IPostMessageService, PostMessageService>();
services.AddScoped<ITenantService, TenantService>(); services.AddScoped<ITenantService, TenantService>();
services.AddScoped<IFileService, FileService>();
services.Configure<MailSettings>(Configuration.GetSection("MailSettings")); services.Configure<MailSettings>(Configuration.GetSection("MailSettings"));
services.AddScoped<IEmailService, EmailService>(); services.AddScoped<IEmailService, EmailService>();
...@@ -148,6 +151,14 @@ namespace Tsi1.Api ...@@ -148,6 +151,14 @@ namespace Tsi1.Api
c.DocumentTitle = "Tsi1 api"; c.DocumentTitle = "Tsi1 api";
}); });
var provider = new FileExtensionContentTypeProvider();
app.UseStaticFiles(new StaticFileOptions()
{
ContentTypeProvider = provider,
RequestPath = "/static"
}
); ;
app.UseRouting(); app.UseRouting();
app.UseCors(x => x.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader()); app.UseCors(x => x.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader());
......
using System;
using System.Collections.Generic;
using System.Text;
namespace Tsi1.BusinessLayer.Dtos
{
public class FileDto
{
public int Id { get; set; }
public string Name { get; set; }
public string Path { get; set; }
}
}
...@@ -38,5 +38,9 @@ namespace Tsi1.BusinessLayer.Helpers ...@@ -38,5 +38,9 @@ namespace Tsi1.BusinessLayer.Helpers
public const string DuplicateTenantName = "Ya existe una Facultad con nombre '{0}'"; public const string DuplicateTenantName = "Ya existe una Facultad con nombre '{0}'";
public const string InvalidUserType = "Tipo de usuario invalido '{0}'"; public const string InvalidUserType = "Tipo de usuario invalido '{0}'";
public const string ErrorSavingFile = "No se pudo guardar el archivo '{0}'";
public const string ErrorDeletingFile = "No se pudo borrar el archivo '{0}'";
public const string DuplicateFile = "Ya existe el archivo '{0}'";
} }
} }
...@@ -30,6 +30,7 @@ namespace Tsi1.BusinessLayer.Helpers ...@@ -30,6 +30,7 @@ namespace Tsi1.BusinessLayer.Helpers
CreateMap<Tenant, TenantPreviewDto>(); CreateMap<Tenant, TenantPreviewDto>();
CreateMap<Tenant, TenantCreateDto>(); CreateMap<Tenant, TenantCreateDto>();
CreateMap<UserType, UserTypeDto>(); CreateMap<UserType, UserTypeDto>();
CreateMap<File, FileDto>();
CreateMap<ForumCreateDto, Forum>(); CreateMap<ForumCreateDto, Forum>();
CreateMap<ForumPreviewDto, Forum>(); CreateMap<ForumPreviewDto, Forum>();
...@@ -49,6 +50,7 @@ namespace Tsi1.BusinessLayer.Helpers ...@@ -49,6 +50,7 @@ namespace Tsi1.BusinessLayer.Helpers
CreateMap<TenantPreviewDto, Tenant>(); CreateMap<TenantPreviewDto, Tenant>();
CreateMap<TenantCreateDto, Tenant>(); CreateMap<TenantCreateDto, Tenant>();
CreateMap<UserTypeDto, UserType>(); CreateMap<UserTypeDto, UserType>();
CreateMap<FileDto, File>();
} }
} }
} }
using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using Tsi1.BusinessLayer.Dtos;
using Tsi1.BusinessLayer.Helpers;
namespace Tsi1.BusinessLayer.Interfaces
{
public interface IFileService
{
//Task<ServiceResult<FileDto>> SaveFile(IFormFile file);
Task<ServiceResult<FileDto>> Create(IFormFile file, int tenantId, int courseId);
}
}
using AutoMapper;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using Tsi1.BusinessLayer.Dtos;
using Tsi1.BusinessLayer.Helpers;
using Tsi1.BusinessLayer.Interfaces;
using Tsi1.DataLayer;
using Tsi1.DataLayer.Entities;
namespace Tsi1.BusinessLayer.Services
{
public class FileService : IFileService
{
private readonly Tsi1Context _context;
private readonly IMapper _mapper;
private readonly string _path;
public FileService(Tsi1Context context, IMapper mapper, IHostingEnvironment hostingEnvironment)
{
_context = context;
_mapper = mapper;
_path = hostingEnvironment.ContentRootPath + "/wwwroot";
}
public async Task<ServiceResult<string>> SaveFile(IFormFile file, string filePath)
{
var result = new ServiceResult<string>();
var fileStream = new FileStream(filePath, FileMode.Create);
try
{
await file.CopyToAsync(fileStream);
}
catch (Exception)
{
result.HasError = true;
result.Message = string.Format(ErrorMessages.ErrorSavingFile, filePath);
return result;
}
finally
{
fileStream.Close();
}
return result;
}
public async Task<ServiceResult<FileDto>> Create(IFormFile file, int tenantId, int courseId)
{
var result = new ServiceResult<FileDto>();
var tenant = await _context.Tenants
.FirstOrDefaultAsync(x => x.Id == tenantId);
var course = await _context.Courses
.FirstOrDefaultAsync(x => x.Id == courseId);
var filePath = Path.Combine(_path, tenant.Name, course.Name, file.FileName);
var existingFile = await _context.Files.FirstOrDefaultAsync(x => x.Path == filePath);
if (existingFile != null)
{
result.HasError = true;
result.Message = string.Format(ErrorMessages.DuplicateFile, filePath);
return result;
}
var newFile = new DataLayer.Entities.File
{
Name = file.FileName,
Path = filePath.Replace(_path, ""),
};
_context.Add(newFile);
await _context.SaveChangesAsync();
var resultSaveFile = await this.SaveFile(file, filePath);
if (resultSaveFile.HasError)
{
result.HasError = true;
result.Message = resultSaveFile.Message;
_context.Remove(newFile);
await _context.SaveChangesAsync();
return result;
}
var fileDto = _mapper.Map<FileDto>(newFile);
result.Data = fileDto;
return result;
}
public async Task<ServiceResult<bool>> DeleteFile(string filePath)
{
var result = new ServiceResult<bool>();
try
{
using (var fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.None, 4096, FileOptions.DeleteOnClose)) {}
}
catch (Exception)
{
result.HasError = true;
result.Message = string.Format(ErrorMessages.ErrorDeletingFile, filePath);
return result;
}
return result;
}
}
}
...@@ -8,6 +8,8 @@ ...@@ -8,6 +8,8 @@
<PackageReference Include="AutoMapper" Version="10.1.1" /> <PackageReference Include="AutoMapper" Version="10.1.1" />
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="8.1.0" /> <PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="8.1.0" />
<PackageReference Include="MailKit" Version="2.9.0" /> <PackageReference Include="MailKit" Version="2.9.0" />
<PackageReference Include="Microsoft.AspNetCore.Hosting.Abstractions" Version="2.2.0" />
<PackageReference Include="Microsoft.AspNetCore.Http.Features" Version="3.1.9" />
<PackageReference Include="MimeKit" Version="2.9.2" /> <PackageReference Include="MimeKit" Version="2.9.2" />
</ItemGroup> </ItemGroup>
......
using System;
using System.Collections.Generic;
using System.Text;
namespace Tsi1.DataLayer.Entities
{
public class File
{
public int Id { get; set; }
public string Name { get; set; }
public string Path { get; set; }
}
}
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using System;
using System.Collections.Generic;
using System.Text;
using Tsi1.DataLayer.Entities;
namespace Tsi1.DataLayer.EntityConfiguration
{
public class FileConfiguration : IEntityTypeConfiguration<File>
{
public void Configure(EntityTypeBuilder<File> builder)
{
builder.HasKey(x => x.Id);
builder.Property(x => x.Name)
.IsRequired()
.HasColumnType("character varying(50)");
builder.Property(x => x.Path)
.IsRequired()
.HasColumnType("character varying(1000)");
builder.HasIndex(x => x.Path)
.IsUnique();
}
}
}
// <auto-generated />
using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
using Tsi1.DataLayer;
namespace Tsi1.DataLayer.Migrations
{
[DbContext(typeof(Tsi1Context))]
[Migration("20201101163611_add-File-entity")]
partial class addFileentity
{
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn)
.HasAnnotation("ProductVersion", "3.1.4")
.HasAnnotation("Relational:MaxIdentifierLength", 63);
modelBuilder.Entity("Tsi1.DataLayer.Entities.Course", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("integer")
.HasAnnotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn);
b.Property<string>("Name")
.IsRequired()
.HasColumnType("character varying(50)");
b.Property<int>("TenantId")
.HasColumnType("integer");
b.HasKey("Id");
b.HasIndex("TenantId");
b.HasIndex("Name", "TenantId")
.IsUnique();
b.ToTable("Courses");
});
modelBuilder.Entity("Tsi1.DataLayer.Entities.File", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("integer")
.HasAnnotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn);
b.Property<string>("Name")
.IsRequired()
.HasColumnType("character varying(50)");
b.Property<string>("Path")
.IsRequired()
.HasColumnType("character varying(100)");
b.HasKey("Id");
b.HasIndex("Path")
.IsUnique();
b.ToTable("Files");
});
modelBuilder.Entity("Tsi1.DataLayer.Entities.Forum", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("integer")
.HasAnnotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn);
b.Property<int>("CourseId")
.HasColumnType("integer");
b.Property<string>("Name")
.IsRequired()
.HasColumnType("character varying(50)");
b.HasKey("Id");
b.HasIndex("CourseId", "Name")
.IsUnique();
b.ToTable("Forums");
});
modelBuilder.Entity("Tsi1.DataLayer.Entities.ForumUser", b =>
{
b.Property<int>("ForumId")
.HasColumnType("integer");
b.Property<int>("UserId")
.HasColumnType("integer");
b.HasKey("ForumId", "UserId");
b.HasIndex("UserId");
b.ToTable("ForumUsers");
});
modelBuilder.Entity("Tsi1.DataLayer.Entities.Post", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("integer")
.HasAnnotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn);
b.Property<DateTime>("Date")
.HasColumnType("timestamp without time zone");
b.Property<int>("ForumId")
.HasColumnType("integer");
b.Property<string>("Title")
.IsRequired()
.HasColumnType("character varying(100)");
b.Property<int>("UserId")
.HasColumnType("integer");
b.HasKey("Id");
b.HasIndex("UserId");
b.HasIndex("ForumId", "Title")
.IsUnique();
b.ToTable("Posts");
});
modelBuilder.Entity("Tsi1.DataLayer.Entities.PostMessage", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("integer")
.HasAnnotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn);
b.Property<string>("Content")
.IsRequired()
.HasColumnType("character varying(10485760)");
b.Property<DateTime>("Date")
.HasColumnType("timestamp without time zone");
b.Property<int>("PostId")
.HasColumnType("integer");
b.Property<int>("UserId")
.HasColumnType("integer");
b.HasKey("Id");
b.HasIndex("PostId");
b.HasIndex("UserId");
b.ToTable("PostMessages");
});
modelBuilder.Entity("Tsi1.DataLayer.Entities.Professor", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("integer")
.HasAnnotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn);
b.Property<string>("IdentityCard")
.IsRequired()
.HasColumnType("character varying(50)");
b.Property<int>("TenantId")
.HasColumnType("integer");
b.HasKey("Id");
b.HasIndex("TenantId");
b.HasIndex("IdentityCard", "TenantId")
.IsUnique();
b.ToTable("Professors");
});
modelBuilder.Entity("Tsi1.DataLayer.Entities.ProfessorCourse", b =>
{
b.Property<int>("ProfessorId")
.HasColumnType("integer");
b.Property<int>("CourseId")
.HasColumnType("integer");
b.HasKey("ProfessorId", "CourseId");
b.HasIndex("CourseId");
b.ToTable("ProfessorCourses");
});
modelBuilder.Entity("Tsi1.DataLayer.Entities.Student", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("integer")
.HasAnnotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn);
b.Property<int>("Age")
.HasColumnType("integer");
b.Property<string>("IdentityCard")
.IsRequired()
.HasColumnType("character varying(50)");
b.Property<int>("TenantId")
.HasColumnType("integer");
b.HasKey("Id");
b.HasIndex("TenantId");
b.HasIndex("IdentityCard", "TenantId")
.IsUnique();
b.ToTable("Students");
});
modelBuilder.Entity("Tsi1.DataLayer.Entities.StudentCourse", b =>
{
b.Property<int>("StudentId")
.HasColumnType("integer");
b.Property<int>("CourseId")
.HasColumnType("integer");
b.HasKey("StudentId", "CourseId");
b.HasIndex("CourseId");
b.ToTable("StudentCourses");
});
modelBuilder.Entity("Tsi1.DataLayer.Entities.Tenant", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("integer")
.HasAnnotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn);
b.Property<string>("Name")
.IsRequired()
.HasColumnType("character varying(50)");
b.HasKey("Id");
b.HasIndex("Name")
.IsUnique();
b.ToTable("Tenants");
});
modelBuilder.Entity("Tsi1.DataLayer.Entities.User", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("integer")
.HasAnnotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn);
b.Property<string>("Email")
.IsRequired()
.HasColumnType("character varying(255)");
b.Property<string>("FirstName")
.IsRequired()
.HasColumnType("character varying(255)");
b.Property<string>("LastName")
.IsRequired()
.HasColumnType("character varying(255)");
b.Property<string>("Password")
.IsRequired()
.HasColumnType("character varying(255)");
b.Property<int?>("ProfessorId")
.HasColumnType("integer");
b.Property<int?>("StudentId")
.HasColumnType("integer");
b.Property<int>("TenantId")
.HasColumnType("integer");
b.Property<int>("UserTypeId")
.HasColumnType("integer");
b.Property<string>("Username")
.IsRequired()
.HasColumnType("character varying(50)");
b.HasKey("Id");
b.HasIndex("ProfessorId")
.IsUnique();
b.HasIndex("StudentId")
.IsUnique();
b.HasIndex("TenantId");
b.HasIndex("UserTypeId");
b.HasIndex("Username", "TenantId")
.IsUnique();
b.ToTable("Users");
});
modelBuilder.Entity("Tsi1.DataLayer.Entities.UserType", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("integer")
.HasAnnotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn);
b.Property<string>("Name")
.IsRequired()
.HasColumnType("character varying(50)");
b.HasKey("Id");
b.HasIndex("Name")
.IsUnique();
b.ToTable("UserTypes");
});
modelBuilder.Entity("Tsi1.DataLayer.Entities.Course", b =>
{
b.HasOne("Tsi1.DataLayer.Entities.Tenant", "Tenant")
.WithMany("Courses")
.HasForeignKey("TenantId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
});
modelBuilder.Entity("Tsi1.DataLayer.Entities.Forum", b =>
{
b.HasOne("Tsi1.DataLayer.Entities.Course", "Course")
.WithMany("Forums")
.HasForeignKey("CourseId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
});
modelBuilder.Entity("Tsi1.DataLayer.Entities.ForumUser", b =>
{
b.HasOne("Tsi1.DataLayer.Entities.Forum", "Forum")
.WithMany("ForumUsers")
.HasForeignKey("ForumId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("Tsi1.DataLayer.Entities.User", "User")
.WithMany("ForumUsers")
.HasForeignKey("UserId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
});
modelBuilder.Entity("Tsi1.DataLayer.Entities.Post", b =>
{
b.HasOne("Tsi1.DataLayer.Entities.Forum", "Forum")
.WithMany("Posts")
.HasForeignKey("ForumId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("Tsi1.DataLayer.Entities.User", "User")
.WithMany("Posts")
.HasForeignKey("UserId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
});
modelBuilder.Entity("Tsi1.DataLayer.Entities.PostMessage", b =>
{
b.HasOne("Tsi1.DataLayer.Entities.Post", "Post")
.WithMany("PostMessages")
.HasForeignKey("PostId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("Tsi1.DataLayer.Entities.User", "User")
.WithMany("PostMessages")
.HasForeignKey("UserId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
});
modelBuilder.Entity("Tsi1.DataLayer.Entities.Professor", b =>
{
b.HasOne("Tsi1.DataLayer.Entities.Tenant", "Tenant")
.WithMany("Professors")
.HasForeignKey("TenantId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
});
modelBuilder.Entity("Tsi1.DataLayer.Entities.ProfessorCourse", b =>
{
b.HasOne("Tsi1.DataLayer.Entities.Course", "Course")
.WithMany("ProfessorCourses")
.HasForeignKey("CourseId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("Tsi1.DataLayer.Entities.Professor", "Professor")
.WithMany("ProfessorCourses")
.HasForeignKey("ProfessorId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
});
modelBuilder.Entity("Tsi1.DataLayer.Entities.Student", b =>
{
b.HasOne("Tsi1.DataLayer.Entities.Tenant", "Tenant")
.WithMany("Students")
.HasForeignKey("TenantId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
});
modelBuilder.Entity("Tsi1.DataLayer.Entities.StudentCourse", b =>
{
b.HasOne("Tsi1.DataLayer.Entities.Course", "Course")
.WithMany("StudentCourses")
.HasForeignKey("CourseId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("Tsi1.DataLayer.Entities.Student", "Student")
.WithMany("StudentCourses")
.HasForeignKey("StudentId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
});
modelBuilder.Entity("Tsi1.DataLayer.Entities.User", b =>
{
b.HasOne("Tsi1.DataLayer.Entities.Professor", "Professor")
.WithOne("User")
.HasForeignKey("Tsi1.DataLayer.Entities.User", "ProfessorId");
b.HasOne("Tsi1.DataLayer.Entities.Student", "Student")
.WithOne("User")
.HasForeignKey("Tsi1.DataLayer.Entities.User", "StudentId");
b.HasOne("Tsi1.DataLayer.Entities.Tenant", "Tenant")
.WithMany("Users")
.HasForeignKey("TenantId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("Tsi1.DataLayer.Entities.UserType", "UserType")
.WithMany()
.HasForeignKey("UserTypeId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
});
#pragma warning restore 612, 618
}
}
}
using Microsoft.EntityFrameworkCore.Migrations;
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
namespace Tsi1.DataLayer.Migrations
{
public partial class addFileentity : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "Files",
columns: table => new
{
Id = table.Column<int>(nullable: false)
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
Name = table.Column<string>(type: "character varying(50)", nullable: false),
Path = table.Column<string>(type: "character varying(100)", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Files", x => x.Id);
});
migrationBuilder.CreateIndex(
name: "IX_Files_Path",
table: "Files",
column: "Path",
unique: true);
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "Files");
}
}
}
// <auto-generated />
using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
using Tsi1.DataLayer;
namespace Tsi1.DataLayer.Migrations
{
[DbContext(typeof(Tsi1Context))]
[Migration("20201101164258_change-File-Path-Property")]
partial class changeFilePathProperty
{
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn)
.HasAnnotation("ProductVersion", "3.1.4")
.HasAnnotation("Relational:MaxIdentifierLength", 63);
modelBuilder.Entity("Tsi1.DataLayer.Entities.Course", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("integer")
.HasAnnotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn);
b.Property<string>("Name")
.IsRequired()
.HasColumnType("character varying(50)");
b.Property<int>("TenantId")
.HasColumnType("integer");
b.HasKey("Id");
b.HasIndex("TenantId");
b.HasIndex("Name", "TenantId")
.IsUnique();
b.ToTable("Courses");
});
modelBuilder.Entity("Tsi1.DataLayer.Entities.File", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("integer")
.HasAnnotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn);
b.Property<string>("Name")
.IsRequired()
.HasColumnType("character varying(50)");
b.Property<string>("Path")
.IsRequired()
.HasColumnType("character varying(1000)");
b.HasKey("Id");
b.HasIndex("Path")
.IsUnique();
b.ToTable("Files");
});
modelBuilder.Entity("Tsi1.DataLayer.Entities.Forum", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("integer")
.HasAnnotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn);
b.Property<int>("CourseId")
.HasColumnType("integer");
b.Property<string>("Name")
.IsRequired()
.HasColumnType("character varying(50)");
b.HasKey("Id");
b.HasIndex("CourseId", "Name")
.IsUnique();
b.ToTable("Forums");
});
modelBuilder.Entity("Tsi1.DataLayer.Entities.ForumUser", b =>
{
b.Property<int>("ForumId")
.HasColumnType("integer");
b.Property<int>("UserId")
.HasColumnType("integer");
b.HasKey("ForumId", "UserId");
b.HasIndex("UserId");
b.ToTable("ForumUsers");
});
modelBuilder.Entity("Tsi1.DataLayer.Entities.Post", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("integer")
.HasAnnotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn);
b.Property<DateTime>("Date")
.HasColumnType("timestamp without time zone");
b.Property<int>("ForumId")
.HasColumnType("integer");
b.Property<string>("Title")
.IsRequired()
.HasColumnType("character varying(100)");
b.Property<int>("UserId")
.HasColumnType("integer");
b.HasKey("Id");
b.HasIndex("UserId");
b.HasIndex("ForumId", "Title")
.IsUnique();
b.ToTable("Posts");
});
modelBuilder.Entity("Tsi1.DataLayer.Entities.PostMessage", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("integer")
.HasAnnotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn);
b.Property<string>("Content")
.IsRequired()
.HasColumnType("character varying(10485760)");
b.Property<DateTime>("Date")
.HasColumnType("timestamp without time zone");
b.Property<int>("PostId")
.HasColumnType("integer");
b.Property<int>("UserId")
.HasColumnType("integer");
b.HasKey("Id");
b.HasIndex("PostId");
b.HasIndex("UserId");
b.ToTable("PostMessages");
});
modelBuilder.Entity("Tsi1.DataLayer.Entities.Professor", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("integer")
.HasAnnotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn);
b.Property<string>("IdentityCard")
.IsRequired()
.HasColumnType("character varying(50)");
b.Property<int>("TenantId")
.HasColumnType("integer");
b.HasKey("Id");
b.HasIndex("TenantId");
b.HasIndex("IdentityCard", "TenantId")
.IsUnique();
b.ToTable("Professors");
});
modelBuilder.Entity("Tsi1.DataLayer.Entities.ProfessorCourse", b =>
{
b.Property<int>("ProfessorId")
.HasColumnType("integer");
b.Property<int>("CourseId")
.HasColumnType("integer");
b.HasKey("ProfessorId", "CourseId");
b.HasIndex("CourseId");
b.ToTable("ProfessorCourses");
});
modelBuilder.Entity("Tsi1.DataLayer.Entities.Student", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("integer")
.HasAnnotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn);
b.Property<int>("Age")
.HasColumnType("integer");
b.Property<string>("IdentityCard")
.IsRequired()
.HasColumnType("character varying(50)");
b.Property<int>("TenantId")
.HasColumnType("integer");
b.HasKey("Id");
b.HasIndex("TenantId");
b.HasIndex("IdentityCard", "TenantId")
.IsUnique();
b.ToTable("Students");
});
modelBuilder.Entity("Tsi1.DataLayer.Entities.StudentCourse", b =>
{
b.Property<int>("StudentId")
.HasColumnType("integer");
b.Property<int>("CourseId")
.HasColumnType("integer");
b.HasKey("StudentId", "CourseId");
b.HasIndex("CourseId");
b.ToTable("StudentCourses");
});
modelBuilder.Entity("Tsi1.DataLayer.Entities.Tenant", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("integer")
.HasAnnotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn);
b.Property<string>("Name")
.IsRequired()
.HasColumnType("character varying(50)");
b.HasKey("Id");
b.HasIndex("Name")
.IsUnique();
b.ToTable("Tenants");
});
modelBuilder.Entity("Tsi1.DataLayer.Entities.User", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("integer")
.HasAnnotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn);
b.Property<string>("Email")
.IsRequired()
.HasColumnType("character varying(255)");
b.Property<string>("FirstName")
.IsRequired()
.HasColumnType("character varying(255)");
b.Property<string>("LastName")
.IsRequired()
.HasColumnType("character varying(255)");
b.Property<string>("Password")
.IsRequired()
.HasColumnType("character varying(255)");
b.Property<int?>("ProfessorId")
.HasColumnType("integer");
b.Property<int?>("StudentId")
.HasColumnType("integer");
b.Property<int>("TenantId")
.HasColumnType("integer");
b.Property<int>("UserTypeId")
.HasColumnType("integer");
b.Property<string>("Username")
.IsRequired()
.HasColumnType("character varying(50)");
b.HasKey("Id");
b.HasIndex("ProfessorId")
.IsUnique();
b.HasIndex("StudentId")
.IsUnique();
b.HasIndex("TenantId");
b.HasIndex("UserTypeId");
b.HasIndex("Username", "TenantId")
.IsUnique();
b.ToTable("Users");
});
modelBuilder.Entity("Tsi1.DataLayer.Entities.UserType", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("integer")
.HasAnnotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn);
b.Property<string>("Name")
.IsRequired()
.HasColumnType("character varying(50)");
b.HasKey("Id");
b.HasIndex("Name")
.IsUnique();
b.ToTable("UserTypes");
});
modelBuilder.Entity("Tsi1.DataLayer.Entities.Course", b =>
{
b.HasOne("Tsi1.DataLayer.Entities.Tenant", "Tenant")
.WithMany("Courses")
.HasForeignKey("TenantId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
});
modelBuilder.Entity("Tsi1.DataLayer.Entities.Forum", b =>
{
b.HasOne("Tsi1.DataLayer.Entities.Course", "Course")
.WithMany("Forums")
.HasForeignKey("CourseId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
});
modelBuilder.Entity("Tsi1.DataLayer.Entities.ForumUser", b =>
{
b.HasOne("Tsi1.DataLayer.Entities.Forum", "Forum")
.WithMany("ForumUsers")
.HasForeignKey("ForumId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("Tsi1.DataLayer.Entities.User", "User")
.WithMany("ForumUsers")
.HasForeignKey("UserId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
});
modelBuilder.Entity("Tsi1.DataLayer.Entities.Post", b =>
{
b.HasOne("Tsi1.DataLayer.Entities.Forum", "Forum")
.WithMany("Posts")
.HasForeignKey("ForumId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("Tsi1.DataLayer.Entities.User", "User")
.WithMany("Posts")
.HasForeignKey("UserId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
});
modelBuilder.Entity("Tsi1.DataLayer.Entities.PostMessage", b =>
{
b.HasOne("Tsi1.DataLayer.Entities.Post", "Post")
.WithMany("PostMessages")
.HasForeignKey("PostId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("Tsi1.DataLayer.Entities.User", "User")
.WithMany("PostMessages")
.HasForeignKey("UserId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
});
modelBuilder.Entity("Tsi1.DataLayer.Entities.Professor", b =>
{
b.HasOne("Tsi1.DataLayer.Entities.Tenant", "Tenant")
.WithMany("Professors")
.HasForeignKey("TenantId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
});
modelBuilder.Entity("Tsi1.DataLayer.Entities.ProfessorCourse", b =>
{
b.HasOne("Tsi1.DataLayer.Entities.Course", "Course")
.WithMany("ProfessorCourses")
.HasForeignKey("CourseId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("Tsi1.DataLayer.Entities.Professor", "Professor")
.WithMany("ProfessorCourses")
.HasForeignKey("ProfessorId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
});
modelBuilder.Entity("Tsi1.DataLayer.Entities.Student", b =>
{
b.HasOne("Tsi1.DataLayer.Entities.Tenant", "Tenant")
.WithMany("Students")
.HasForeignKey("TenantId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
});
modelBuilder.Entity("Tsi1.DataLayer.Entities.StudentCourse", b =>
{
b.HasOne("Tsi1.DataLayer.Entities.Course", "Course")
.WithMany("StudentCourses")
.HasForeignKey("CourseId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("Tsi1.DataLayer.Entities.Student", "Student")
.WithMany("StudentCourses")
.HasForeignKey("StudentId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
});
modelBuilder.Entity("Tsi1.DataLayer.Entities.User", b =>
{
b.HasOne("Tsi1.DataLayer.Entities.Professor", "Professor")
.WithOne("User")
.HasForeignKey("Tsi1.DataLayer.Entities.User", "ProfessorId");
b.HasOne("Tsi1.DataLayer.Entities.Student", "Student")
.WithOne("User")
.HasForeignKey("Tsi1.DataLayer.Entities.User", "StudentId");
b.HasOne("Tsi1.DataLayer.Entities.Tenant", "Tenant")
.WithMany("Users")
.HasForeignKey("TenantId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("Tsi1.DataLayer.Entities.UserType", "UserType")
.WithMany()
.HasForeignKey("UserTypeId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
});
#pragma warning restore 612, 618
}
}
}
using Microsoft.EntityFrameworkCore.Migrations;
namespace Tsi1.DataLayer.Migrations
{
public partial class changeFilePathProperty : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AlterColumn<string>(
name: "Path",
table: "Files",
type: "character varying(1000)",
nullable: false,
oldClrType: typeof(string),
oldType: "character varying(100)");
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.AlterColumn<string>(
name: "Path",
table: "Files",
type: "character varying(100)",
nullable: false,
oldClrType: typeof(string),
oldType: "character varying(1000)");
}
}
}
...@@ -43,6 +43,29 @@ namespace Tsi1.DataLayer.Migrations ...@@ -43,6 +43,29 @@ namespace Tsi1.DataLayer.Migrations
b.ToTable("Courses"); b.ToTable("Courses");
}); });
modelBuilder.Entity("Tsi1.DataLayer.Entities.File", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("integer")
.HasAnnotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn);
b.Property<string>("Name")
.IsRequired()
.HasColumnType("character varying(50)");
b.Property<string>("Path")
.IsRequired()
.HasColumnType("character varying(1000)");
b.HasKey("Id");
b.HasIndex("Path")
.IsUnique();
b.ToTable("Files");
});
modelBuilder.Entity("Tsi1.DataLayer.Entities.Forum", b => modelBuilder.Entity("Tsi1.DataLayer.Entities.Forum", b =>
{ {
b.Property<int>("Id") b.Property<int>("Id")
......
...@@ -21,6 +21,7 @@ namespace Tsi1.DataLayer ...@@ -21,6 +21,7 @@ namespace Tsi1.DataLayer
public DbSet<PostMessage> PostMessages { get; set; } public DbSet<PostMessage> PostMessages { get; set; }
public DbSet<Tenant> Tenants { get; set; } public DbSet<Tenant> Tenants { get; set; }
public DbSet<ForumUser> ForumUsers { get; set; } public DbSet<ForumUser> ForumUsers { get; set; }
public DbSet<File> Files { get; set; }
public Tsi1Context(DbContextOptions options) : base(options) { } public Tsi1Context(DbContextOptions options) : base(options) { }
...@@ -39,6 +40,7 @@ namespace Tsi1.DataLayer ...@@ -39,6 +40,7 @@ namespace Tsi1.DataLayer
modelBuilder.ApplyConfiguration(new PostMessageConfiguration()); modelBuilder.ApplyConfiguration(new PostMessageConfiguration());
modelBuilder.ApplyConfiguration(new TenantConfiguration()); modelBuilder.ApplyConfiguration(new TenantConfiguration());
modelBuilder.ApplyConfiguration(new ForumUserConfiguration()); modelBuilder.ApplyConfiguration(new ForumUserConfiguration());
modelBuilder.ApplyConfiguration(new FileConfiguration());
} }
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment