using CMSDAL.DTO.ContentType; using CMSDAL.Query.ContentType; using GB5Shared.DTO.Framework.Criteria; using GB5Shared.DTO.Framework.Login; using GB5Shared.QueryExecutor; using GB5Shared.Resource.Response; using GB5Shared.ResponseStandard; using GB5Shared.Validation; using Microsoft.Extensions.Logging; using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace CMSDAL.CustomCode.ContentType { public class ContentTypeDAL : IContentTypeDAL { private readonly IQueryExecutor _QueryExecutor; private readonly IValidation _Validation; private readonly ILogger _logger; public ContentTypeDAL(IQueryExecutor QueryExecutor, IValidation Validation, ILogger logger) { _QueryExecutor = QueryExecutor; _Validation = Validation; _logger = logger; } public async Task GetContentType(int ContentTypeId, LoginDTO LoginDTO) { try { string Sql = ContentTypeQB.GET_CONTENTTYPE; var Parameterss = new { ContentTypeId = ContentTypeId }; ContentTypeDTO ContentTypeDTOs = await _QueryExecutor.QuerySingleAsync(LoginDTO, Sql, Parameterss); return JsonConvert.SerializeObject(ContentTypeDTOs); } catch (Exception) { throw; } } public async Task GetSelectListContentType(int FirstNumber, int MaxResult, CriteriaDTO CriteriaDTO, LoginDTO LoginDTO, bool IsCount = false) { try { string Json = ""; if (!IsCount) { string Sql = ContentTypeQB.GET_SELECTLIST_CONTENTTYPE; var Parameters = new { firstnumber = FirstNumber, maxresult = MaxResult }; IEnumerable ContentTypeDTO = await _QueryExecutor.QueryAsync(LoginDTO, Sql, Parameters); Json = JsonConvert.SerializeObject(ContentTypeDTO); } else { int Count = 0; Json = Count.ToString(); } return Json; } catch (Exception) { throw; } } public async Task SaveContentType(ContentTypeDTO ContentTypeDTO, LoginDTO LoginDTO) { try { string Sql = ContentTypeQB.SAVE_CONTENTTYPE; return await _QueryExecutor.ExecuteAsync(LoginDTO, Sql, ContentTypeDTO); } catch (Exception ex) { string Error = await _Validation.HandleException(ex, $"{ErrorResponse.SaveErrorMessage}"); throw new Exception(Error); } } public async Task UpdateContentType(ContentTypeDTO ContentTypeDTO, LoginDTO LoginDTO) { try { string Sql = ContentTypeQB.UPDATE_CONTENTTYPE; return await _QueryExecutor.ExecuteAsync(LoginDTO, Sql, ContentTypeDTO); } catch (Exception ex) { string Error = await _Validation.HandleException(ex, $"{ErrorResponse.UpdateErrorMessage}"); throw new Exception(Error); } } public async Task DeleteContentType(int ContentTypeId, LoginDTO LoginDTO) { try { string Sql = ContentTypeQB.DELETE_CONTENTTYPE; var Parameters = new { ContentTypeid = ContentTypeId }; int result = await _QueryExecutor.ExecuteAsync(LoginDTO, Sql, Parameters); if (result > 0) { return $"{SuccessResponse.DeleteSuccessMessage}"; } else { return $"{ErrorResponse.DeleteNotFoundMessage}"; } } catch (Exception ex) { string Error = await _Validation.HandleException(ex, $"{ErrorResponse.DeleteErrorMessage}"); throw new Exception(Error); } } public async Task GetContentTypeList(CriteriaDTO criteriaDTO, LoginDTO LoginDTO) { try { string sql = ContentTypeQB.GET_CONTENTTYPE_LIST; var result = await _QueryExecutor.QueryAsync(LoginDTO, sql, null!); return JsonConvert.SerializeObject(result); } catch (Exception ex) { _logger.LogError(ex, "GetContentTypeList failed"); throw; } } } }