Ejemplo n.º 1
0
        public override async Task <BaseResponse> DeleteAsync(HolidayData request, ServerCallContext context)
        {
            try
            {
                if (request is null)
                {
                    await _holidaysRepository.DeleteAsync(null);
                }

                Holiday holiday = FromRpcModel(request);
                int     result  = await _holidaysRepository.DeleteAsync(holiday);

                if (result == 0)
                {
                    throw new Exception("Holiday has not been deleted");
                }

                BaseResponse response = new()
                {
                    Code         = Code.Success,
                    ErrorMessage = string.Empty,
                    DataId       = holiday.Id
                };

                LogData logData = new()
                {
                    CallSide         = nameof(HolidaysService),
                    CallerMethodName = nameof(DeleteAsync),
                    CreatedOn        = _dateTimeUtil.GetCurrentDateTime(),
                    Request          = request,
                    Response         = response
                };
                _logger.AddLog(logData);

                return(response);
            }
            catch (NullReferenceException nrex)
            {
                LogData logData = new()
                {
                    CallSide         = nameof(HolidaysService),
                    CallerMethodName = nameof(DeleteAsync),
                    CreatedOn        = _dateTimeUtil.GetCurrentDateTime(),
                    Request          = request,
                    Response         = nrex
                };
                _logger.AddErrorLog(logData);

                return(new BaseResponse
                {
                    Code = Code.DataError,
                    ErrorMessage = nrex.Message
                });
            }
            catch (InvalidOperationException ioex)
            {
                LogData logData = new()
                {
                    CallSide         = nameof(HolidaysService),
                    CallerMethodName = nameof(DeleteAsync),
                    CreatedOn        = _dateTimeUtil.GetCurrentDateTime(),
                    Request          = request,
                    Response         = ioex
                };
                _logger.AddErrorLog(logData);

                return(new BaseResponse
                {
                    Code = Code.DataError,
                    ErrorMessage = ioex.Message
                });
            }
            catch (DbUpdateException duex)
            {
                LogData logData = new()
                {
                    CallSide         = nameof(HolidaysService),
                    CallerMethodName = nameof(DeleteAsync),
                    CreatedOn        = _dateTimeUtil.GetCurrentDateTime(),
                    Request          = request,
                    Response         = duex
                };
                _logger.AddErrorLog(logData);

                return(new BaseResponse
                {
                    Code = Code.DbError,
                    ErrorMessage = "An error occured while deleting holiday"
                });
            }
            catch (Exception ex)
            {
                LogData logData = new()
                {
                    CallSide         = nameof(HolidaysService),
                    CallerMethodName = nameof(DeleteAsync),
                    CreatedOn        = _dateTimeUtil.GetCurrentDateTime(),
                    Request          = request,
                    Response         = ex
                };
                _logger.AddErrorLog(logData);

                return(new BaseResponse
                {
                    Code = Code.UnknownError,
                    ErrorMessage = ex.Message
                });
            }
        }