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 { EFCoreLanguageRepository _repository; string responseMessage = string.Empty; public LanguagesController(EFCoreLanguageRepository repository) : base(repository) { _repository = repository; } /// /// This endpoint will retrieve all active languages(SU,A,T,S). /// /// [HttpGet] [Authorize(Roles = "SuperAdmin,Admin,Teacher,Student")] public override IActionResult GetAll() { IActionResult returnResponse; //------------------------------------------------------------------------------------- List 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; } /// /// This endpoint will retrieve the active language by id (SU,A,T) but only SU can get inactive language. /// [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; } /// /// This endpoint will retrieve the active language by code (SU,A,T) but only SU can get inactive language. /// [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; } /// /// This endpoint will add a new language(SU). /// /// [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; } /// /// This endpoint will update the language(SU). /// /// [HttpPost("{id}/Update")] [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; } /// /// This endpoint will restore the deleted languages(SU). /// /// [HttpPost("{id}/Restore")] [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; } } }