using CMSDAL.DTO.Content; using CMSDAL.Query.Content; using GB5Shared.DTO.Framework.Criteria; using GB5Shared.DTO.Framework.Login; using GB5Shared.QueryExecutor; using GB5Shared.Resource.Response; using GB5Shared.ResponseStandard; using GB5Shared.Validation; using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Data.Common; using System.Linq; using System.Text; using System.Threading.Tasks; namespace CMSDAL.CustomCode.Content { public class ContentDAL : IContentDAL { private readonly IQueryExecutor _QueryExecutor; private readonly IValidation _Validation; public ContentDAL(IQueryExecutor QueryExecutor, IValidation Validation) { _QueryExecutor = QueryExecutor; _Validation = Validation; } public async Task GetContentBySlug(string Slug, LoginDTO LoginDTO) { var parameters = new { Slug = Slug, TenantId = LoginDTO.ClientId }; var results = await _QueryExecutor.QueryAsync(LoginDTO, ContentQB.GET_CONTENT_BY_SLUG, parameters); return results.FirstOrDefault(); } public async Task GetContent(int ContentId, LoginDTO LoginDTO) { try { string Sql = ContentQB.GET_CONTENT; var Parameterss = new { ContentId = ContentId, TenantId = LoginDTO.ClientId }; ContentDTO ContentDTOs = await _QueryExecutor.QuerySingleAsync(LoginDTO, Sql, Parameterss); return JsonConvert.SerializeObject(ContentDTOs); } catch (Exception) { throw; } } public async Task GetSelectListContent(int FirstNumber, int MaxResult, CriteriaDTO CriteriaDTO, LoginDTO LoginDTO, bool IsCount = false) { try { string Json = ""; if (!IsCount) { string Sql = ContentQB.GET_SELECTLIST_CONTENT; var Parameters = new { firstnumber = FirstNumber, maxresult = MaxResult }; IEnumerable ContentPicklistDTO = await _QueryExecutor.QueryAsync(LoginDTO, Sql, Parameters); Json = JsonConvert.SerializeObject(ContentPicklistDTO); } else { int Count = 0; Json = Count.ToString(); } return Json; } catch (Exception) { throw; } } public async Task SaveContent(ContentDTO ContentDTO, LoginDTO LoginDTO, DbTransaction? tx = null, CancellationToken ct = default) { try { string Sql = ContentQB.SAVE_CONTENT; int newContentId = await _QueryExecutor.ExecuteIdentityAsync(LoginDTO, Sql, ContentDTO, tx); ContentDTO.ContentId = newContentId; return newContentId; } catch (Exception ex) { string Error = await _Validation.HandleException(ex, $"{ErrorResponse.SaveErrorMessage}"); throw new Exception(Error); } } public async Task UpdateContent(ContentDTO ContentDTO, LoginDTO LoginDTO, DbTransaction? tx = null, CancellationToken ct = default) { try { string Sql = ContentQB.UPDATE_CONTENT; await _QueryExecutor.ExecuteAsync(LoginDTO, Sql, ContentDTO, tx, ct); return ContentDTO.ContentId; } catch (Exception ex) { string Error = await _Validation.HandleException(ex, $"{ErrorResponse.UpdateErrorMessage}"); throw new Exception(Error); } } public async Task DeleteContent(int ContentId, LoginDTO LoginDTO) { try { string Sql = ContentQB.DELETE_CONTENT; var Parameters = new { Contentid = ContentId }; 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 GetContentListAsync(int spaceId, int contentTypeId, int contentStatus, int authorId, DateTime? fromDate, DateTime? toDate, LoginDTO loginDTO) { try { var result = await _QueryExecutor.QueryAsync(loginDTO, ContentQB.GET_CONTENT_LIST, new { TenantId = loginDTO.ClientId, SpaceId = spaceId, ContentTypeId = contentTypeId, ContentStatus = contentStatus, AuthorId = authorId, FromDate = fromDate, ToDate = toDate }); return JsonConvert.SerializeObject(result); } catch (Exception) { throw; } } public async Task SearchContentAsync(string searchText, int spaceId, int contentTypeId, LoginDTO loginDTO) { try { var result = await _QueryExecutor.QueryAsync(loginDTO, ContentQB.SEARCH_CONTENT, new { TenantId = loginDTO.ClientId, SearchText = searchText, SpaceId = spaceId, ContentTypeId = contentTypeId }); return JsonConvert.SerializeObject(result); } catch (Exception) { throw; } } public async Task UpdateContentStatusAsync(int contentId, byte status, LoginDTO loginDTO, DbTransaction? tx = null, CancellationToken ct = default) { try { await _QueryExecutor.ExecuteAsync(loginDTO, ContentQB.UPDATE_CONTENT_STATUS, new { ContentId = contentId, ContentStatus = status, TenantId = loginDTO.ClientId, ModifiedById = loginDTO.UserId }, tx, ct); return SuccessResponse.UpdateSuccessMessage; } catch (Exception) { throw; } } public async Task UpdateContentStatusWithPublishAsync(int contentId, byte status, LoginDTO loginDTO, DbTransaction? tx = null, CancellationToken ct = default) { try { await _QueryExecutor.ExecuteAsync(loginDTO, ContentQB.UPDATE_CONTENT_STATUS_PUBLISH, new { ContentId = contentId, ContentStatus = status, TenantId = loginDTO.ClientId, ModifiedById = loginDTO.UserId }, tx, ct); return SuccessResponse.UpdateSuccessMessage; } catch (Exception) { throw; } } public async Task GetContentStatusAsync(int contentId, LoginDTO loginDTO, DbTransaction? tx = null, CancellationToken ct = default) { try { var result = await _QueryExecutor.QueryAsync(loginDTO, ContentQB.GET_CONTENT_STATUS, new { ContentId = contentId, TenantId = loginDTO.ClientId }, transaction: tx, cancellationToken: ct); return result.FirstOrDefault(); } catch (Exception) { throw; } } } }