From dd2ac4fb1ff7838fd55e62fdbb9367ffef0ce7bb Mon Sep 17 00:00:00 2001 From: Lucca Santangelo <luccasant95@gmail.com> Date: Sat, 12 Dec 2020 17:09:26 -0300 Subject: [PATCH] asd --- .../Tsi1.Api/Controllers/CourseController.cs | 6 +++ .../Controllers/DataLoadController.cs | 41 +++++++++++++++ .../Tsi1.BusinessLayer/DataLoad/DataLoad.cs | 36 ++++++++++++- .../Tsi1.BusinessLayer/DataLoad/IDataLoad.cs | 3 ++ .../Dtos/CourseCreateBedeliaDto.cs | 11 ++++ .../Dtos/UserCreateBedeliaDto.cs | 13 +++++ .../Interfaces/IBedeliaService.cs | 4 +- .../Services/BedeliaService.cs | 51 +++++++++++++++++++ 8 files changed, 162 insertions(+), 3 deletions(-) create mode 100644 Tsi1.Api/Tsi1.BusinessLayer/Dtos/CourseCreateBedeliaDto.cs create mode 100644 Tsi1.Api/Tsi1.BusinessLayer/Dtos/UserCreateBedeliaDto.cs diff --git a/Tsi1.Api/Tsi1.Api/Controllers/CourseController.cs b/Tsi1.Api/Tsi1.Api/Controllers/CourseController.cs index 2610d44..b9d543f 100644 --- a/Tsi1.Api/Tsi1.Api/Controllers/CourseController.cs +++ b/Tsi1.Api/Tsi1.Api/Controllers/CourseController.cs @@ -56,6 +56,12 @@ namespace Tsi1.Api.Controllers var tenantId = int.Parse(HttpContext.User.Claims.FirstOrDefault(x => x.Type == "TenantId").Value); newCourse.TenantId = tenantId; + var bedeliaResult = await _bedeliaService.IsValidCourse(newCourse.Name); + if (bedeliaResult.HasError) + { + return BadRequest(bedeliaResult.Message); + } + var result = await _courseService.Create(newCourse); if (result.HasError) { diff --git a/Tsi1.Api/Tsi1.Api/Controllers/DataLoadController.cs b/Tsi1.Api/Tsi1.Api/Controllers/DataLoadController.cs index d97bd21..23c30fa 100644 --- a/Tsi1.Api/Tsi1.Api/Controllers/DataLoadController.cs +++ b/Tsi1.Api/Tsi1.Api/Controllers/DataLoadController.cs @@ -28,5 +28,46 @@ namespace Tsi1.Api.Controllers return Ok(); } + + [AllowAnonymous] + [HttpPost("LoadStudentsInBedelia")] + public async Task<IActionResult> LoadStudentsInBedelia() + { + try + { + var result = await _dataLoad.LoadStudentsInBedelia(); + if (result.HasError) + { + return BadRequest(result.Message); + } + + } + catch (Exception ex) + { + return BadRequest(ex.Message); + } + + return Ok(); + } + + [AllowAnonymous] + [HttpPost("LoadCoursesInBedelia")] + public async Task<IActionResult> LoadCoursesInBedelia() + { + try + { + var result = await _dataLoad.LoadCoursesInBedelia(); + if (result.HasError) + { + return BadRequest(result.Message); + } + } + catch (Exception ex) + { + return BadRequest(ex.Message); + } + + return Ok(); + } } } diff --git a/Tsi1.Api/Tsi1.BusinessLayer/DataLoad/DataLoad.cs b/Tsi1.Api/Tsi1.BusinessLayer/DataLoad/DataLoad.cs index 4562c75..ff63b39 100644 --- a/Tsi1.Api/Tsi1.BusinessLayer/DataLoad/DataLoad.cs +++ b/Tsi1.Api/Tsi1.BusinessLayer/DataLoad/DataLoad.cs @@ -1,5 +1,7 @@ -using System; +using Microsoft.EntityFrameworkCore; +using System; using System.Collections.Generic; +using System.Linq; using System.Text; using System.Threading.Tasks; using Tsi1.BusinessLayer.Dtos; @@ -18,13 +20,15 @@ namespace Tsi1.BusinessLayer.DataLoad private readonly ICourseService _courseService; private readonly ISectionService _sectionService; private readonly ISectionItemService _sectionItemService; + private readonly IBedeliaService _bedeliaService; public DataLoad(Tsi1Context context, ITenantService tenantService, IUserService userService, ICourseService courseService, ISectionService sectionService, - ISectionItemService sectionItemService) + ISectionItemService sectionItemService, + IBedeliaService bedeliaService) { _context = context; _tenantService = tenantService; @@ -32,6 +36,34 @@ namespace Tsi1.BusinessLayer.DataLoad _courseService = courseService; _sectionService = sectionService; _sectionItemService = sectionItemService; + _bedeliaService = bedeliaService; + } + + public async Task<ServiceResult<bool>> LoadStudentsInBedelia() + { + var users = await (from u in _context.Users + join s in _context.Students + on u.StudentId equals s.Id + select new UserCreateBedeliaDto + { + IdentityCard = s.IdentityCard, + Name = string.Join(" ", u.FirstName, u.LastName), + Password = u.Password + }).ToListAsync(); + + return await _bedeliaService.CreateUsers(users); + } + + public async Task<ServiceResult<bool>> LoadCoursesInBedelia() + { + var courses = await _context.Courses.Select(x => + new CourseCreateBedeliaDto() + { + Name = x.Name + }) + .ToListAsync(); + + return await _bedeliaService.CreateCourses(courses); } public async Task LoadDataAsync() diff --git a/Tsi1.Api/Tsi1.BusinessLayer/DataLoad/IDataLoad.cs b/Tsi1.Api/Tsi1.BusinessLayer/DataLoad/IDataLoad.cs index 613fbe6..b2a80aa 100644 --- a/Tsi1.Api/Tsi1.BusinessLayer/DataLoad/IDataLoad.cs +++ b/Tsi1.Api/Tsi1.BusinessLayer/DataLoad/IDataLoad.cs @@ -2,11 +2,14 @@ using System.Collections.Generic; using System.Text; using System.Threading.Tasks; +using Tsi1.BusinessLayer.Helpers; namespace Tsi1.BusinessLayer.DataLoad { public interface IDataLoad { Task LoadDataAsync(); + Task<ServiceResult<bool>> LoadStudentsInBedelia(); + Task<ServiceResult<bool>> LoadCoursesInBedelia(); } } diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Dtos/CourseCreateBedeliaDto.cs b/Tsi1.Api/Tsi1.BusinessLayer/Dtos/CourseCreateBedeliaDto.cs new file mode 100644 index 0000000..5897c34 --- /dev/null +++ b/Tsi1.Api/Tsi1.BusinessLayer/Dtos/CourseCreateBedeliaDto.cs @@ -0,0 +1,11 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Tsi1.BusinessLayer.Dtos +{ + public class CourseCreateBedeliaDto + { + public string Name { get; set; } + } +} diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Dtos/UserCreateBedeliaDto.cs b/Tsi1.Api/Tsi1.BusinessLayer/Dtos/UserCreateBedeliaDto.cs new file mode 100644 index 0000000..207f76b --- /dev/null +++ b/Tsi1.Api/Tsi1.BusinessLayer/Dtos/UserCreateBedeliaDto.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Tsi1.BusinessLayer.Dtos +{ + public class UserCreateBedeliaDto + { + public string IdentityCard { get; set; } + public string Name { get; set; } + public string Password { get; set; } + } +} diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/IBedeliaService.cs b/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/IBedeliaService.cs index 88a6f29..c2f6782 100644 --- a/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/IBedeliaService.cs +++ b/Tsi1.Api/Tsi1.BusinessLayer/Interfaces/IBedeliaService.cs @@ -10,7 +10,9 @@ namespace Tsi1.BusinessLayer.Interfaces public interface IBedeliaService { Task<ServiceResult<bool>> IsValidUser(string identityCard); - Task<ServiceResult<bool>> CloseRecord(string courseName, List<UserGradeDto> userGrades); + Task<ServiceResult<bool>> IsValidCourse(string courseName); + Task<ServiceResult<bool>> CreateUsers(List<UserCreateBedeliaDto> users); + Task<ServiceResult<bool>> CreateCourses(List<CourseCreateBedeliaDto> users); } } diff --git a/Tsi1.Api/Tsi1.BusinessLayer/Services/BedeliaService.cs b/Tsi1.Api/Tsi1.BusinessLayer/Services/BedeliaService.cs index cd7a08a..1ce82ad 100644 --- a/Tsi1.Api/Tsi1.BusinessLayer/Services/BedeliaService.cs +++ b/Tsi1.Api/Tsi1.BusinessLayer/Services/BedeliaService.cs @@ -57,5 +57,56 @@ namespace Tsi1.BusinessLayer.Services return result; } + + public async Task<ServiceResult<bool>> IsValidCourse(string courseName) + { + var result = new ServiceResult<bool>(); + + var response = await _httpClient.GetAsync($"api/Courses/{courseName}"); + if (response.StatusCode != HttpStatusCode.OK) + { + result.HasError = true; + var errorMessage = await response.Content.ReadAsStringAsync(); + result.AddMessage(errorMessage); + } + + return result; + } + + public async Task<ServiceResult<bool>> CreateUsers(List<UserCreateBedeliaDto> users) + { + var result = new ServiceResult<bool>(); + + var jsonInString = JsonConvert.SerializeObject(users); + var model = new StringContent(jsonInString, Encoding.UTF8, "application/json"); + + var response = await _httpClient.PostAsync("api/Users", model); + if (response.StatusCode != HttpStatusCode.OK) + { + result.HasError = true; + var errorMessage = await response.Content.ReadAsStringAsync(); + result.AddMessage(errorMessage); + } + + return result; + } + + public async Task<ServiceResult<bool>> CreateCourses(List<CourseCreateBedeliaDto> courses) + { + var result = new ServiceResult<bool>(); + + var jsonInString = JsonConvert.SerializeObject(courses); + var model = new StringContent(jsonInString, Encoding.UTF8, "application/json"); + + var response = await _httpClient.PostAsync("api/Courses", model); + if (response.StatusCode != HttpStatusCode.OK) + { + result.HasError = true; + var errorMessage = await response.Content.ReadAsStringAsync(); + result.AddMessage(errorMessage); + } + + return result; + } } } -- GitLab