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

data load

parent d522a209
No related branches found
No related tags found
2 merge requests!26Develop,!22Feature/data load
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.DataLoad;
namespace Tsi1.Api.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class DataLoadController : ControllerBase
{
private readonly IDataLoad _dataLoad;
public DataLoadController(IDataLoad dataLoad)
{
_dataLoad = dataLoad;
}
[AllowAnonymous]
[HttpGet("LoadData")]
public async Task<IActionResult> LoadData()
{
await _dataLoad.LoadDataAsync();
return Ok();
}
}
}
......@@ -23,6 +23,7 @@ using Tsi1.BusinessLayer.Helpers;
using Tsi1.BusinessLayer.HostedServices;
using Tsi1.BusinessLayer.Interfaces;
using Tsi1.BusinessLayer.Services;
using Tsi1.BusinessLayer.DataLoad;
using Tsi1.DataLayer;
using Tsi1.DataLayer.MongoDbConfiguration;
......@@ -79,6 +80,7 @@ namespace Tsi1.Api
services.AddScoped<ISectionService, SectionService>();
services.AddScoped<ISectionItemService, SectionItemService>();
services.AddScoped<ISectionItemTypeService, SectionItemTypeService>();
services.AddScoped<IDataLoad, DataLoad>();
services.Configure<MailSettings>(Configuration.GetSection("MailSettings"));
services.AddScoped<IEmailService, EmailService>();
......
using System;
using System.Collections.Generic;
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.DataLoad
{
public class DataLoad : IDataLoad
{
private readonly Tsi1Context _context;
private readonly IUserTypeService _userTypeService;
private readonly ITenantService _tenantService;
private readonly IUserService _userService;
private readonly ICourseService _courseService;
private readonly ISectionItemTypeService _sectionItemTypeService;
private readonly ISectionService _sectionService;
private readonly ISectionItemService _sectionItemService;
public DataLoad(Tsi1Context context,
IUserTypeService userTypeService,
ITenantService tenantService,
IUserService userService,
ICourseService courseService,
ISectionItemTypeService sectionItemTypeService,
ISectionService sectionService,
ISectionItemService sectionItemService)
{
_context = context;
_userTypeService = userTypeService;
_tenantService = tenantService;
_userService = userService;
_courseService = courseService;
_sectionItemTypeService = sectionItemTypeService;
_sectionService = sectionService;
_sectionItemService = sectionItemService;
}
public async Task LoadDataAsync()
{
// TENANTS
var adminTenant = new TenantCreateDto
{
Name = TenantAdmin.Name,
};
var adminTenantId = await _tenantService.Create(adminTenant);
var fingTenant = new TenantCreateDto
{
Name = "fing",
};
var fingTenantId = await _tenantService.Create(fingTenant);
// USER TYPES
var udelarAdminUserType = new UserType
{
Name = UserTypes.UdelarAdmin,
};
_context.Add(udelarAdminUserType);
var facultyAdminUserType = new UserType
{
Name = UserTypes.FacultyAdmin,
};
_context.Add(facultyAdminUserType);
var professorUserType = new UserType
{
Name = UserTypes.Professor,
};
_context.Add(professorUserType);
var studentUserType = new UserType
{
Name = UserTypes.Student,
};
_context.Add(studentUserType);
await _context.SaveChangesAsync();
// USERS
var userAdminUdelar = new UserRegisterDto
{
UserTypeId = udelarAdminUserType.Id,
Username = "admin",
Password = "admin",
FirstName = "admin",
LastName = "admin",
Email = "admin@mail.com",
IdentityCard = "12345678-9",
Age = 44
};
await _userService.Create(userAdminUdelar, udelarAdminUserType.Name, adminTenantId.Data.Id);
var userStudent1 = new UserRegisterDto
{
UserTypeId = studentUserType.Id,
Username = "enzo",
Password = "enzo",
FirstName = "Enzo",
LastName = "Santangelo",
Email = "enzo@mail.com",
IdentityCard = "13242344-5",
Age = 25
};
await _userService.Create(userStudent1, studentUserType.Name, fingTenantId.Data.Id);
var userStudent2 = new UserRegisterDto
{
UserTypeId = studentUserType.Id,
Username = "mathias",
Password = "mathias",
FirstName = "Mathias",
LastName = "Martinez",
Email = "mathias@mail.com",
IdentityCard = "3782346-5",
Age = 26
};
await _userService.Create(userStudent2, studentUserType.Name, fingTenantId.Data.Id);
var userProfessor = new UserRegisterDto
{
UserTypeId = professorUserType.Id,
Username = "prof1",
Password = "prof1",
FirstName = "Juan",
LastName = "Perez",
Email = "jp@mail.com",
IdentityCard = "98754342-5",
Age = 34
};
await _userService.Create(userProfessor, professorUserType.Name, fingTenantId.Data.Id);
var userFingAdmin = new UserRegisterDto
{
UserTypeId = facultyAdminUserType.Id,
Username = "fing",
Password = "fing",
FirstName = "fing",
LastName = "fing",
Email = "fing@mail.com",
IdentityCard = "89547821-5",
Age = 45
};
await _userService.Create(userFingAdmin, facultyAdminUserType.Name, fingTenantId.Data.Id);
// SECTION ITEM TYPES
var sectionItemTypeFile = new SectionItemType
{
Name = SectionItemTypes.File,
};
_context.SectionItemTypes.Add(sectionItemTypeFile);
var sectionItemTypeForum = new SectionItemType
{
Name = SectionItemTypes.Forum,
};
_context.SectionItemTypes.Add(sectionItemTypeForum);
await _context.SaveChangesAsync();
// COURSES
var course1Dto = new CourseCreateDto
{
Name = "Calculo 1",
TenantId = fingTenantId.Data.Id
};
var course1 = await _courseService.Create(course1Dto);
var course2Dto = new CourseCreateDto
{
Name = "GAL 1",
TenantId = fingTenantId.Data.Id
};
var course2 = await _courseService.Create(course2Dto);
var course3Dto = new CourseCreateDto
{
Name = "Fisica 1",
TenantId = fingTenantId.Data.Id
};
var course3 = await _courseService.Create(course3Dto);
// SECTIONS
var section1 = new SectionCreateDto
{
CourseId = course1.Data.Id,
Name = "General",
Order = 1
};
var section1Id = await _sectionService.Create(section1);
var section2 = new SectionCreateDto
{
CourseId = course2.Data.Id,
Name = "General",
Order = 1
};
var section2Id = await _sectionService.Create(section2);
var section3 = new SectionCreateDto
{
CourseId = course1.Data.Id,
Name = "Tema 1",
Order = 2
};
var section3Id = await _sectionService.Create(section3);
// SECTION ITEMS
var sectionItem1 = new SectionItemCreateDto
{
SectionId = section1Id.Data,
Order = 1,
SectionItemTypeId = sectionItemTypeForum.Id,
Forum = new ForumCreateDto
{
Name = "Novedades"
}
};
await _sectionItemService.Create(sectionItem1);
var sectionItem2 = new SectionItemCreateDto
{
SectionId = section2Id.Data,
Order = 1,
SectionItemTypeId = sectionItemTypeForum.Id,
Forum = new ForumCreateDto
{
Name = "Novedades"
}
};
await _sectionItemService.Create(sectionItem2);
var sectionItem3 = new SectionItemCreateDto
{
SectionId = section1Id.Data,
Order = 2,
SectionItemTypeId = sectionItemTypeForum.Id,
Forum = new ForumCreateDto
{
Name = "General"
}
};
await _sectionItemService.Create(sectionItem3);
var sectionItem4 = new SectionItemCreateDto
{
SectionId = section3Id.Data,
Order = 1,
SectionItemTypeId = sectionItemTypeForum.Id,
Forum = new ForumCreateDto
{
Name = "Tema 1"
}
};
await _sectionItemService.Create(sectionItem4);
}
}
}
\ No newline at end of file
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
namespace Tsi1.BusinessLayer.DataLoad
{
public interface IDataLoad
{
Task LoadDataAsync();
}
}
......@@ -9,7 +9,7 @@ namespace Tsi1.BusinessLayer.Interfaces
{
public interface ISectionService
{
Task<ServiceResult<bool>> Create(SectionCreateDto newSection);
Task<ServiceResult<int>> Create(SectionCreateDto newSection);
Task<ServiceResult<bool>> Delete(int sectionId);
Task<ServiceResult<bool>> OrderSections(List<OrderDto> orderDtos);
Task<ServiceResult<bool>> Modify(int sectionId, string name);
......
......@@ -24,9 +24,9 @@ namespace Tsi1.BusinessLayer.Services
_mapper = mapper;
}
public async Task<ServiceResult<bool>> Create(SectionCreateDto newSection)
public async Task<ServiceResult<int>> Create(SectionCreateDto newSection)
{
var result = new ServiceResult<bool>();
var result = new ServiceResult<int>();
var course = await _context.Courses.FirstOrDefaultAsync(x => x.Id == newSection.CourseId);
if (course == null)
......@@ -40,6 +40,7 @@ namespace Tsi1.BusinessLayer.Services
_context.Sections.Add(section);
await _context.SaveChangesAsync();
result.Data = section.Id;
return result;
}
......
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