using FrameworkDAL.DTO.Comment; using FrameworkDAL.Query.Comment; using GB5Shared.DTO.Framework.Login; using GB5Shared.QueryExecutor; using GB5Shared.ResponseStandard; using Newtonsoft.Json; namespace FrameworkDAL.CustomCode.Comment { public class CommentDAL : ICommentDAL { private readonly IQueryExecutor _queryExecutor; public CommentDAL(IQueryExecutor queryExecutor) { _queryExecutor = queryExecutor; } public async Task GetComment(int CommentId, LoginDTO LoginDTO) { string json = ""; try { string sql = CommentQB.GET_COMMENT; var parameters = new { commentid = CommentId }; CommentDTO CommentDTOs = await _queryExecutor.QuerySingleAsync(LoginDTO, sql, parameters); json = JsonConvert.SerializeObject(CommentDTOs); return json; } catch (Exception ex) { throw new Exception("Error while fetching Comment details", ex); } finally { json = null; } } public async Task SaveComment(CommentDTO CommentDTO, LoginDTO LoginDTO) { try { const string sql = CommentQB.SAVE_COMMENT; return await _queryExecutor.ExecuteAsync(LoginDTO, sql, CommentDTO); } catch (Exception ex) { throw new Exception($"Error while saving Comment (CommentId: {CommentDTO.CommentId})", ex); } } public async Task UpdateComment(CommentDTO CommentDTO, LoginDTO LoginDTO) { try { const string sql = CommentQB.UPDATE_COMMENT; return await _queryExecutor.ExecuteAsync(LoginDTO, sql, CommentDTO); } catch (Exception ex) { throw new Exception($"Error while updating Comment (CommentId: {CommentDTO.CommentId})", ex); } } public async Task> DeleteComment(int CommentId, LoginDTO LoginDTO) { try { string sql = CommentQB.DELETE_COMMENT; var parameters = new { CommentId = CommentId }; var result = await _queryExecutor.ExecuteAsync(LoginDTO, sql, parameters); if (result > 0) { return Result.Success($"Comment deleted successfully with CommentId {CommentId}"); } else { return Result.Failure($"Comment not found or could not be deleted. CommentId: {CommentId}"); } } catch (Exception ex) { throw new Exception($"Error while deleting Comment (CommentId: {CommentId})", ex); } } } }