practicekea_backend/microservices/admin/V1/Controllers/LanguagesController.cs

189 lines
7.7 KiB
C#
Raw Permalink Normal View History

2024-12-02 13:24:34 +00:00
using System.Collections.Generic;
using Microsoft.AspNetCore.Authorization;
using System.Security.Claims;
using Microsoft.AspNetCore.Mvc;
using OnlineAssessment.Common;
using OnlineAssessment.Data.EFCore;
using OnlineAssessment.Domain.Models;
using OnlineAssessment.Domain.ViewModels;
namespace OnlineAssessment.V1.Controllers
{
//[Authorize]
[ApiController]
[ApiVersion("1.0")]
[Route("v{version:apiVersion}/[controller]")]
public class LanguagesController : BaseController<Languages, EFCoreLanguageRepository>
{
EFCoreLanguageRepository _repository;
string responseMessage = string.Empty;
public LanguagesController(EFCoreLanguageRepository repository) : base(repository)
{
_repository = repository;
}
/// <summary>
/// This endpoint will retrieve all active languages(SU,A,T,S).
/// </summary>
/// <returns></returns>
[HttpGet]
[Authorize(Roles = "SuperAdmin,Admin,Teacher,Student")]
public override IActionResult GetAll()
{
IActionResult returnResponse;
//-------------------------------------------------------------------------------------
List<Language> iList = _repository.GetListOfLanguages();
//-------------------------------------------------------------------------------------
if (iList == null)
{
responseMessage = _repository.GetMessageByCode(Message.NoData.ToString());
returnResponse = Ok(ReturnResponse.GetFailureStatus(responseMessage));
}
else
{
returnResponse = Ok(ReturnResponse.GetSuccessStatus(iList));
}
return returnResponse;
}
/// <summary>
/// This endpoint will retrieve the active language by id (SU,A,T) but only SU can get inactive language.
/// </summary>
[HttpGet("{id}")]
[Authorize(Roles = "SuperAdmin,Admin,Teacher")]
public override IActionResult Get(int id)
{
IActionResult returnResponse;
int role_id = int.Parse(Security.GetValueFromToken("RoleId", HttpContext.User.Identity as ClaimsIdentity));
//-------------------------------------------------------------------------------------
Language entity = _repository.GetLanguageById(id);
//-------------------------------------------------------------------------------------
//Only superadmin can retrive deleted ExamType
if (entity == null || (entity.IsActive == false && role_id != 1))
{
responseMessage = _repository.GetMessageByCode(Message.ObjectNotFound.ToString(), Constant.Language);
returnResponse = Ok(ReturnResponse.GetFailureStatus(responseMessage));
}
else
{
returnResponse = Ok(ReturnResponse.GetSuccessStatus(entity));
}
return returnResponse;
}
/// <summary>
/// This endpoint will retrieve the active language by code (SU,A,T) but only SU can get inactive language.
/// </summary>
[HttpGet("ByCode/{code}")]
[Authorize(Roles = "SuperAdmin,Admin,Teacher")]
public IActionResult GetByCode(string code)
{
IActionResult returnResponse;
int role_id = int.Parse(Security.GetValueFromToken("RoleId", HttpContext.User.Identity as ClaimsIdentity));
//-------------------------------------------------------------------------------------
Language entity = _repository.GetLanguageByCode(code);
//-------------------------------------------------------------------------------------
//Only superadmin can retrive deleted ExamType
if (entity == null || (entity.IsActive == false && role_id != 1))
{
responseMessage = _repository.GetMessageByCode(Message.ObjectNotFound.ToString(), Constant.Language);
returnResponse = Ok(ReturnResponse.GetFailureStatus(responseMessage));
}
else
{
returnResponse = Ok(ReturnResponse.GetSuccessStatus(entity));
}
return returnResponse;
}
/// <summary>
/// This endpoint will add a new language(SU).
/// </summary>
/// <returns></returns>
[HttpPost]
[Authorize(Roles = "SuperAdmin")]
public IActionResult AddLanguage([FromBody] LanguageAddModel language)
{
IActionResult returnResponse;
int user_id = Security.GetIdFromJwtToken(UserClaim.UserId, HttpContext.User.Identity as ClaimsIdentity);
//-------------------------------------------------------------------------------------
Language newLanguage = _repository.AddLanguage(user_id, language);
//-------------------------------------------------------------------------------------
if (newLanguage.Id > 0) //Successfully Added
{
returnResponse = Ok(ReturnResponse.GetSuccessStatus(newLanguage));
}
else
{
responseMessage = _repository.GetMessageByCode(Message.ObjectNotAdded.ToString(), Constant.Language);
returnResponse = Ok(ReturnResponse.GetFailureStatus(responseMessage));
}
return returnResponse;
}
/// <summary>
/// This endpoint will update the language(SU).
/// </summary>
/// <returns></returns>
2025-10-29 18:18:12 +00:00
[HttpPost("{id}/Update")]
2024-12-02 13:24:34 +00:00
[Authorize(Roles = "SuperAdmin")]
public IActionResult UpdateLanguage(int id, [FromBody] LanguageEditModel theLanguage)
{
IActionResult returnResponse = null;
int user_id = Security.GetIdFromJwtToken(UserClaim.UserId, HttpContext.User.Identity as ClaimsIdentity);
//-------------------------------------------------------------------------------------
Language language = _repository.UpdateLanguage(user_id, id, theLanguage);
//-------------------------------------------------------------------------------------
if (language == null)
{
responseMessage = _repository.GetMessageByCode(Message.ObjectNotUpdated.ToString(), Constant.Language);
returnResponse = Ok(ReturnResponse.GetFailureStatus(responseMessage));
}
else
{
returnResponse = Ok(ReturnResponse.GetSuccessStatus(language));
}
return returnResponse;
}
/// <summary>
/// This endpoint will restore the deleted languages(SU).
/// </summary>
/// <returns></returns>
2025-10-29 18:18:12 +00:00
[HttpPost("{id}/Restore")]
2024-12-02 13:24:34 +00:00
[Authorize(Roles = "SuperAdmin")]
public IActionResult RestoreLanguage(int id)
{
IActionResult returnResponse = null;
int user_id = Security.GetIdFromJwtToken(UserClaim.UserId, HttpContext.User.Identity as ClaimsIdentity);
//-------------------------------------------------------------------------------------
Language language = _repository.RestoreLanguage(user_id, id);
//-------------------------------------------------------------------------------------
if (language == null)
{
responseMessage = _repository.GetMessageByCode(Message.ObjectNotUpdated.ToString(), Constant.Language);
returnResponse = Ok(ReturnResponse.GetFailureStatus(responseMessage));
}
else
{
returnResponse = Ok(ReturnResponse.GetSuccessStatus(language));
}
return returnResponse;
}
}
}