diff --git a/Tsi1.Api/Tsi1.Api/Controllers/CourseController.cs b/Tsi1.Api/Tsi1.Api/Controllers/CourseController.cs index 2610d44b06250e559f0b6710fb0d93f6bf0b23d6..b9d543f7f1c43cff454925076b42bf62cf869ae4 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 d97bd21d6deb6b862fb45fc87fb2e235c8e8ca9c..23c30fa56cbd345847d9cbacf0cc962ba5f5cbcc 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 4562c75fc7f0f6e09222d506d31b41ab1cdcb43b..ff63b39f7f0e0c2aca51014442d273565911a38e 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 613fbe6364554626bc8db4b44ce90f4a1e86725c..b2a80aa5aa70de415a8502aabe6cf504fef967d1 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 0000000000000000000000000000000000000000..5897c34e4c61e4c53166832477411d21fe1adc14 --- /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 0000000000000000000000000000000000000000..207f76b1854967fc95d812bcc1aa0090ef944a00 --- /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 88a6f293779d4dd16356078565ec3010a95dfa37..c2f67820ebbc0a07c25457c02ec1d491f9888191 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 cd7a08a470c3107dcb49d35dbbe0255cbe4e7dae..1ce82adbbfabe824b1dd14240ef283b24c8147c2 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; + } } }