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

add GetById endpoint

parent aaa07520
No related branches found
No related tags found
1 merge request!5Multi tenancy
...@@ -102,5 +102,20 @@ namespace Tsi1.Api.Controllers ...@@ -102,5 +102,20 @@ namespace Tsi1.Api.Controllers
return Ok(result.Data); return Ok(result.Data);
} }
[Authorize(Roles = UserTypes.Student + ", " + UserTypes.Professor)]
[HttpGet("GetById/{userId}")]
public async Task<IActionResult> GetById(int userId)
{
var result = await _userService.GetById(userId);
if (result.HasError)
{
return BadRequest(result.Message);
}
return Ok(result.Data);
}
} }
} }
using System;
using System.Collections.Generic;
using System.Text;
namespace Tsi1.BusinessLayer.Dtos
{
public class ProfessorPreviewDto
{
public string IdentityCard { get; set; }
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace Tsi1.BusinessLayer.Dtos
{
public class StudentPreviewDto
{
public string IdentityCard { get; set; }
public int Age { get; set; }
}
}
...@@ -13,5 +13,9 @@ namespace Tsi1.BusinessLayer.Dtos ...@@ -13,5 +13,9 @@ namespace Tsi1.BusinessLayer.Dtos
public string FirstName { get; set; } public string FirstName { get; set; }
public string LastName { get; set; } public string LastName { get; set; }
public StudentPreviewDto Student { get; set; }
public ProfessorPreviewDto Professor { get; set; }
} }
} }
...@@ -22,6 +22,8 @@ namespace Tsi1.BusinessLayer.Helpers ...@@ -22,6 +22,8 @@ namespace Tsi1.BusinessLayer.Helpers
CreateMap<Message, MessageCreateDto>(); CreateMap<Message, MessageCreateDto>();
CreateMap<User, UserPreviewDto>(); CreateMap<User, UserPreviewDto>();
CreateMap<User, UserRegisterDto>(); CreateMap<User, UserRegisterDto>();
CreateMap<Student, StudentPreviewDto>();
CreateMap<Professor, ProfessorPreviewDto>();
CreateMap<Course, CourseCreateDto>(); CreateMap<Course, CourseCreateDto>();
CreateMap<Course, CoursePreviewDto>(); CreateMap<Course, CoursePreviewDto>();
...@@ -36,8 +38,10 @@ namespace Tsi1.BusinessLayer.Helpers ...@@ -36,8 +38,10 @@ namespace Tsi1.BusinessLayer.Helpers
CreateMap<MessageCreateDto, Message>(); CreateMap<MessageCreateDto, Message>();
CreateMap<UserPreviewDto, User>(); CreateMap<UserPreviewDto, User>();
CreateMap<UserRegisterDto, User>(); CreateMap<UserRegisterDto, User>();
CreateMap<StudentPreviewDto, Student>();
CreateMap<ProfessorPreviewDto, Professor>();
CreateMap<CourseCreateDto, Course>(); CreateMap<CourseCreateDto, Course>();
CreateMap<CoursePreviewDto, Course>(); CreateMap<CoursePreviewDto, Course>();
} }
} }
} }
...@@ -15,5 +15,7 @@ namespace Tsi1.BusinessLayer.Interfaces ...@@ -15,5 +15,7 @@ namespace Tsi1.BusinessLayer.Interfaces
Task<ServiceResult<User>> Create(UserRegisterDto dto, string type); Task<ServiceResult<User>> Create(UserRegisterDto dto, string type);
Task<ServiceResult<List<UserPreviewDto>>> GetAll(int tenantId); Task<ServiceResult<List<UserPreviewDto>>> GetAll(int tenantId);
Task<ServiceResult<UserPreviewDto>> GetById(int userId);
} }
} }
using AutoMapper; using AutoMapper;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Org.BouncyCastle.Math.EC.Rfc7748;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
...@@ -98,5 +99,44 @@ namespace Tsi1.BusinessLayer.Services ...@@ -98,5 +99,44 @@ namespace Tsi1.BusinessLayer.Services
return result; return result;
} }
public async Task<ServiceResult<UserPreviewDto>> GetById(int userId)
{
var result = new ServiceResult<UserPreviewDto>();
var user = await _context.Users
.Include(x => x.UserType)
.Include(x => x.Student)
.Include(x => x.Professor)
.FirstOrDefaultAsync(x => x.Id == userId);
if (user == null)
{
result.HasError = true;
result.Message = string.Format(ErrorMessages.UserDoesNotExist, userId);
return result;
}
var userType = user.UserType.Name;
if (userType == UserTypes.Student && user.Student == null)
{
result.HasError = true;
result.Message = string.Format(ErrorMessages.StudentDoesNotExist, userId);
return result;
}
else if(userType == UserTypes.Professor && user.Professor == null)
{
result.HasError = true;
result.Message = string.Format(ErrorMessages.ProffesorDoesNotExist, userId);
return result;
}
var userDto = _mapper.Map<UserPreviewDto>(user);
result.Data = userDto;
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