public CreateBranchResponse InsertOrUpdateBranch(InsertOrUpdateBranchRequest request) { var stopwatch = System.Diagnostics.Stopwatch.StartNew(); CreateBranchResponse response = new CreateBranchResponse(); Logger.Info(_logMsg.Clear().SetPrefixMsg("Call BranchService.InsertOrUpdateBranch").ToInputLogString()); Logger.Debug("I:--START--:--BranchService.InsertOrUpdateBranch--"); try { bool valid = false; _commonFacade = new CommonFacade(); if (request.Header != null) { valid = _commonFacade.VerifyServiceRequest <Header>(request.Header); response.Header = new Header { reference_no = request.Header.reference_no, service_name = request.Header.service_name, system_code = request.Header.system_code, transaction_date = request.Header.transaction_date }; } Logger.DebugFormat("-- XMLRequest --\n{0}", request.SerializeObject()); var auditLog = new AuditLogEntity(); auditLog.Module = Constants.Module.WebService; auditLog.Action = Constants.AuditAction.CreateBranch; auditLog.IpAddress = ApplicationHelpers.GetClientIP(); #region == Validate Require Field == if (!valid) { response.StatusResponse = new StatusResponse() { ErrorCode = Constants.ErrorCode.CSMBranch001, Status = Constants.StatusResponse.Failed, Description = "Bad Request, the header is not valid" }; return(response); } else { if (string.IsNullOrEmpty(request.BranchCode)) { return(GetReturnErrorRequireField("BranchCode", response)); } if (string.IsNullOrEmpty(request.BranchName)) { return(GetReturnErrorRequireField("BranchName", response)); } if (string.IsNullOrEmpty(request.ChannelCode)) { return(GetReturnErrorRequireField("ChannelCode", response)); } if (request.StartTimeHour < 0 || request.StartTimeHour > 23) { return(GetReturnErrorInvalidFormat("StartTimeHour", "ต้องมีค่าระหว่าง 0 ถึง 23", "5", response)); } if (request.StartTimeMinute < 0 || request.StartTimeMinute > 59) { return(GetReturnErrorInvalidFormat("StartTimeHour", "ต้องมีค่าระหว่าง 0 ถึง 59", "5", response)); } if (request.EndTimeHour < 0 || request.EndTimeHour > 23) { return(GetReturnErrorInvalidFormat("EndTimeHour", "ต้องมีค่าระหว่าง 0 ถึง 23", "5", response)); } if (request.EndTimeMinute < 0 || request.EndTimeMinute > 59) { return(GetReturnErrorInvalidFormat("EndTimeHour", "ต้องมีค่าระหว่าง 0 ถึง 59", "5", response)); } if (request.Status != 0 && request.Status != 1) { return(GetReturnErrorInvalidFormat("Status", "ต้องมีค่าระหว่าง 0 ถึง 1", "5", response)); } } #endregion #region == Validate Code == var channelDataAccess = new ChannelDataAccess(_context); int?channelId = channelDataAccess.GetChannelIdByChannelCode(request.ChannelCode); if (!channelId.HasValue) { response.StatusResponse = new StatusResponse() { ErrorCode = Constants.ErrorCode.CSMBranch003, Status = Constants.StatusResponse.Failed, Description = "Fail to save branch:\r\nไม่พบ Channel Code ในฐานข้อมูล CSM" }; return(response); } var branchDataAccess = new BranchDataAccess(_context); int?upperBranchId = null; if (!string.IsNullOrEmpty(request.UpperBranchCode)) { upperBranchId = branchDataAccess.GetBranchIdByBranchCode(request.UpperBranchCode); if (!upperBranchId.HasValue) { response.StatusResponse = new StatusResponse() { ErrorCode = Constants.ErrorCode.CSMBranch003, Status = Constants.StatusResponse.Failed, Description = "Fail to save branch:\r\nไม่พบ Upper Branch Code ในฐานข้อมูล CSM" }; return(response); } } #endregion var result = branchDataAccess.InsertOrUpdateBranch(request, channelId.Value, upperBranchId); if (result.IsSuccess) { response.StatusResponse = new StatusResponse { ErrorCode = string.Empty, Status = Constants.StatusResponse.Success, Description = "Save successful" }; AppLog.AuditLog(auditLog, LogStatus.Success, response.StatusResponse.Description); } else { response.StatusResponse = new StatusResponse { ErrorCode = Constants.ErrorCode.CSMBranch003, Status = Constants.StatusResponse.Failed, Description = result.ErrorMessage }; AppLog.AuditLog(auditLog, LogStatus.Fail, response.StatusResponse.Description); } } catch (Exception ex) { Logger.Error("Exception occur:\n", ex); response.StatusResponse = new StatusResponse { ErrorCode = Constants.ErrorCode.CSMBranch004, Status = Constants.StatusResponse.Failed, Description = ex.ToString() }; } finally { Logger.DebugFormat("-- XMLResponse --\n{0}", response.SerializeObject()); stopwatch.Stop(); Logger.DebugFormat("O:--Finish--:ElapsedMilliseconds/{0}", stopwatch.ElapsedMilliseconds); } return(response); }
public InsertOrUpdateBranchResponse InsertOrUpdateBranch(InsertOrUpdateBranchRequest request) { try { #region == Validate Require Field == if (string.IsNullOrEmpty(request.BranchCode)) { return(GetReturnErrorRequireField("BranchCode")); } if (string.IsNullOrEmpty(request.BranchName)) { return(GetReturnErrorRequireField("BranchName")); } if (string.IsNullOrEmpty(request.ChannelCode)) { return(GetReturnErrorRequireField("ChannelCode")); } if (request.StartTimeHour < 0 || request.StartTimeHour > 23) { return(GetReturnErrorInvalidFormat("StartTimeHour", "ต้องมีค่าระหว่าง 0 ถึง 23", "5")); } if (request.StartTimeMinute < 0 || request.StartTimeMinute > 59) { return(GetReturnErrorInvalidFormat("StartTimeHour", "ต้องมีค่าระหว่าง 0 ถึง 59", "5")); } if (request.EndTimeHour < 0 || request.EndTimeHour > 23) { return(GetReturnErrorInvalidFormat("EndTimeHour", "ต้องมีค่าระหว่าง 0 ถึง 23", "5")); } if (request.EndTimeMinute < 0 || request.EndTimeMinute > 59) { return(GetReturnErrorInvalidFormat("EndTimeHour", "ต้องมีค่าระหว่าง 0 ถึง 59", "5")); } if (request.Status != 0 && request.Status != 1) { return(GetReturnErrorInvalidFormat("Status", "ต้องมีค่าระหว่าง 0 ถึง 1", "5")); } #endregion #region == Validate Code == var channelDataAccess = new ChannelDataAccess(_context); int?channelId = channelDataAccess.GetChannelIdByChannelCode(request.ChannelCode); if (!channelId.HasValue) { return(new InsertOrUpdateBranchResponse() { IsSuccess = false, ErrorCode = "3", ErrorMessage = "ไม่พบ Channel Code ในฐานข้อมูล CSM", }); } var branchDataAccess = new BranchDataAccess(_context); int?upperBranchId = null; if (!string.IsNullOrEmpty(request.UpperBranchCode)) { upperBranchId = branchDataAccess.GetBranchIdByBranchCode(request.UpperBranchCode); if (!upperBranchId.HasValue) { return(new InsertOrUpdateBranchResponse() { IsSuccess = false, ErrorCode = "4", ErrorMessage = "ไม่พบ Upper Branch Code ในฐานข้อมูล CSM", }); } } #endregion return(branchDataAccess.InsertOrUpdateBranch(request, channelId.Value, upperBranchId)); } catch (Exception ex) { return(new InsertOrUpdateBranchResponse() { IsSuccess = false, ErrorCode = "1", ErrorMessage = ex.Message }); } }