public async Task <IActionResult> Post([FromBody] TravelExpenseDetailDto data) { APIResult apiResult; #region 驗證 DTO 物件的資料一致性 if (!ModelState.IsValid) { apiResult = APIResultFactory.Build(false, StatusCodes.Status200OK, ErrorMessageEnum.傳送過來的資料有問題, payload: null); return(Ok(apiResult)); } #endregion TravelExpenseDetailAdapterModel record = mapper.Map <TravelExpenseDetailAdapterModel>(data); if (record != null) { var result = mapper.Map <TravelExpenseDetailDto>(record); #region 新增記錄前的紀錄完整性檢查 VerifyRecordResult verify = await TravelExpenseDetailService.BeforeAddCheckAsync(record); if (verify.Success == false) { apiResult = APIResultFactory.Build(false, StatusCodes.Status200OK, ErrorMessageMappingHelper.Instance.GetErrorMessage(verify.MessageId), payload: result); return(Ok(apiResult)); } #endregion var verifyRecordResult = await TravelExpenseDetailService.AddAsync(record); if (verifyRecordResult.Success) { apiResult = APIResultFactory.Build(true, StatusCodes.Status201Created, ErrorMessageEnum.None, payload: result); } else { apiResult = APIResultFactory.Build(false, StatusCodes.Status200OK, ErrorMessageEnum.無法新增紀錄, payload: result); } } else { apiResult = APIResultFactory.Build(false, StatusCodes.Status200OK, ErrorMessageEnum.傳送過來的資料有問題, payload: data); } return(Ok(apiResult)); }
public async Task <IActionResult> Post([FromBody] List <ExceptionRecordDto> datas) { APIResult apiResult = new APIResult(); #region 驗證 DTO 物件的資料一致性 #endregion List <ExceptionRecordAdapterModel> records = mapper.Map <List <ExceptionRecordAdapterModel> >(datas); if (records != null && records.Count > 0) { foreach (var record in records) { var result = mapper.Map <ExceptionRecordDto>(record); #region 新增記錄前的紀錄完整性檢查 VerifyRecordResult verify = await ExceptionRecordService.BeforeAddCheckAsync(record); //if (verify.Success == false) //{ // apiResult = APIResultFactory.Build(false, StatusCodes.Status200OK, // ErrorMessageMappingHelper.Instance.GetErrorMessage(verify.MessageId), // payload: result); // return Ok(apiResult); //} #endregion var verifyRecordResult = await ExceptionRecordService.AddAsync(record); if (verifyRecordResult.Success) { apiResult = APIResultFactory.Build(true, StatusCodes.Status201Created, ErrorMessageEnum.None, payload: null); } else { apiResult = APIResultFactory.Build(false, StatusCodes.Status200OK, ErrorMessageEnum.無法新增紀錄, payload: result); } } } else { apiResult = APIResultFactory.Build(false, StatusCodes.Status200OK, ErrorMessageEnum.傳送過來的資料有問題, payload: null); } apiResult = APIResultFactory.Build(true, StatusCodes.Status201Created, ErrorMessageEnum.None, payload: datas); return(Ok(apiResult)); }
public async Task <IActionResult> GetLeaveForm([FromRoute] int id) { var claimSID = User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value; if (claimSID == null) { apiResult = APIResultFactory.Build(false, StatusCodes.Status400BadRequest, ErrorMessageEnum.杖中沒有發現指定使用者ID); return(BadRequest(apiResult)); } UserID = Convert.ToInt32(claimSID); var fooUser = await _context.LobUsers.Include(x => x.Department).FirstOrDefaultAsync(x => x.Id == UserID); if (fooUser == null) { apiResult = APIResultFactory.Build(false, StatusCodes.Status404NotFound, ErrorMessageEnum.沒有發現指定的該使用者資料); return(NotFound(apiResult)); } if (!ModelState.IsValid) { apiResult = APIResultFactory.Build(false, StatusCodes.Status400BadRequest, ErrorMessageEnum.傳送過來的資料有問題, exceptionMessage: $"傳送過來的資料有問題 {ModelState}"); return(BadRequest(apiResult)); } var leaveForm = await _context.LeaveForms.Include(x => x.LeaveFormType) .Include(x => x.User).ThenInclude(x => x.Department) .FirstOrDefaultAsync(x => x.Id == id); if (leaveForm == null) { apiResult = APIResultFactory.Build(false, StatusCodes.Status404NotFound, ErrorMessageEnum.沒有發現指定的請假單); return(NotFound(apiResult)); } else if (leaveForm.User.Id != UserID) { apiResult = APIResultFactory.Build(false, StatusCodes.Status400BadRequest, ErrorMessageEnum.杖Token上標示的使用者與傳送過來的使用者不一致); return(BadRequest(apiResult)); } apiResult = APIResultFactory.Build(true, StatusCodes.Status200OK, ErrorMessageEnum.None, payload: leaveForm.ToLeaveFormResponseDTO()); return(Ok(apiResult)); }
public async Task <IActionResult> PostNotificationToken([FromBody] NotificationTokenRequestDTO notificationToken) { var claimSID = User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value; if (claimSID == null) { apiResult = APIResultFactory.Build(false, StatusCodes.Status400BadRequest, ErrorMessageEnum.杖中沒有發現指定使用者ID); return(BadRequest(apiResult)); } UserID = Convert.ToInt32(claimSID); var fooUser = await _context.LobUsers.Include(x => x.Department).FirstOrDefaultAsync(x => x.Id == UserID); if (fooUser == null) { apiResult = APIResultFactory.Build(false, StatusCodes.Status404NotFound, ErrorMessageEnum.沒有發現指定的該使用者資料); return(NotFound(apiResult)); } NotificationToken NotificationToken = new NotificationToken() { OSType = (OSType)Enum.Parse(typeof(OSType), notificationToken.OSType.ToString()), RegistrationTime = notificationToken.RegistrationTime, Token = notificationToken.Token, User = fooUser, }; _context.NotificationTokens.Add(NotificationToken); await _context.SaveChangesAsync(); NotificationTokenResponseDTO NotificationTokenResponseDTO = new NotificationTokenResponseDTO() { OSType = notificationToken.OSType, RegistrationTime = notificationToken.RegistrationTime, Token = notificationToken.Token, User = new UserDTO() { Id = fooUser.Id }, }; apiResult = APIResultFactory.Build(true, StatusCodes.Status200OK, ErrorMessageEnum.None, payload: NotificationTokenResponseDTO); return(Ok(apiResult)); }
public async Task <IActionResult> PostSuggestion([FromBody] SuggestionRequestDTO suggestionRequestDTO) { var claimSID = User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value; if (claimSID == null) { apiResult = APIResultFactory.Build(false, StatusCodes.Status400BadRequest, ErrorMessageEnum.杖中沒有發現指定使用者ID); return(BadRequest(apiResult)); } UserID = Convert.ToInt32(claimSID); var fooUser = await _context.LobUsers.Include(x => x.Department).FirstOrDefaultAsync(x => x.Id == UserID); if (fooUser == null) { apiResult = APIResultFactory.Build(false, StatusCodes.Status404NotFound, ErrorMessageEnum.沒有發現指定的該使用者資料); return(NotFound(apiResult)); } Suggestion fooObject = new Suggestion() { Subject = suggestionRequestDTO.Subject, Message = suggestionRequestDTO.Message, SubmitTime = suggestionRequestDTO.SubmitTime, User = fooUser, }; _context.Suggestions.Add(fooObject); await _context.SaveChangesAsync(); SuggestionResponseDTO SuggestionResponseDTO = new SuggestionResponseDTO() { Subject = fooObject.Subject, Message = fooObject.Message, SubmitTime = fooObject.SubmitTime, User = new UserDTO() { Id = fooUser.Id }, }; apiResult = APIResultFactory.Build(true, StatusCodes.Status200OK, ErrorMessageEnum.None, payload: SuggestionResponseDTO); return(Ok(apiResult)); }
public async Task <IActionResult> PostLeaveForm([FromBody] LeaveFormRequestDTO leaveForm) { var claimSID = User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value; if (claimSID == null) { apiResult = APIResultFactory.Build(false, StatusCodes.Status400BadRequest, ErrorMessageEnum.杖中沒有發現指定使用者ID); return(BadRequest(apiResult)); } UserID = Convert.ToInt32(claimSID); var fooUser = await _context.LobUsers.Include(x => x.Department).FirstOrDefaultAsync(x => x.Id == UserID); if (fooUser == null) { apiResult = APIResultFactory.Build(false, StatusCodes.Status404NotFound, ErrorMessageEnum.沒有發現指定的該使用者資料); return(NotFound(apiResult)); } if (!ModelState.IsValid) { apiResult = APIResultFactory.Build(false, StatusCodes.Status400BadRequest, ErrorMessageEnum.傳送過來的資料有問題, exceptionMessage: $"傳送過來的資料有問題 {ModelState}"); return(BadRequest(apiResult)); } var fooLeaveFormType = await _context.LeaveFormTypes.FindAsync(leaveForm.leaveFormType.Id); if (fooLeaveFormType == null) { apiResult = APIResultFactory.Build(false, StatusCodes.Status404NotFound, ErrorMessageEnum.沒有發現指定的請假單類別); return(NotFound(apiResult)); } LeaveForm fooLeaveForm = leaveForm.ToLeaveForm(fooUser, fooLeaveFormType); _context.LeaveForms.Add(fooLeaveForm); await _context.SaveChangesAsync(); apiResult = APIResultFactory.Build(true, StatusCodes.Status202Accepted, ErrorMessageEnum.None, payload: leaveForm); return(Accepted(apiResult)); }
public IActionResult GetDepartments() { List <DepartmentResponseDTO> DepartmentResponseDTO = new List <DepartmentResponseDTO>(); foreach (var item in _context.Departments) { DepartmentResponseDTO fooObject = new DepartmentResponseDTO() { Id = item.Id, Name = item.Name }; DepartmentResponseDTO.Add(fooObject); } APIResult apiResult = APIResultFactory.Build(true, StatusCodes.Status200OK, ErrorMessageEnum.None, payload: DepartmentResponseDTO); return(Ok(apiResult)); }
public async Task <IActionResult> GetCommUserGroups() { List <CommUserGroupResponseDTO> CommUserGroupResponseDTO = new List <CommUserGroupResponseDTO>(); foreach (var item in await _context.CommUserGroups.ToListAsync()) { CommUserGroupResponseDTO fooObject = new CommUserGroupResponseDTO() { Id = item.Id, Name = item.Name }; CommUserGroupResponseDTO.Add(fooObject); } APIResult apiResult = APIResultFactory.Build(true, StatusCodes.Status200OK, ErrorMessageEnum.None, payload: CommUserGroupResponseDTO); return(Ok(apiResult)); }
public IActionResult GetLeaveFormTypes() { List <LeaveFormTypeResponseDTO> LeaveFormTypeResponseDTO = new List <LeaveFormTypeResponseDTO>(); foreach (var item in _context.LeaveFormTypes) { LeaveFormTypeResponseDTO fooObject = new LeaveFormTypeResponseDTO() { Id = item.Id, Name = item.Name }; LeaveFormTypeResponseDTO.Add(fooObject); } APIResult apiResult = APIResultFactory.Build(true, StatusCodes.Status200OK, ErrorMessageEnum.None, payload: LeaveFormTypeResponseDTO); return(Ok(apiResult)); }
public async Task <IActionResult> GetSuggestions() { var claimSID = User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value; if (claimSID == null) { apiResult = APIResultFactory.Build(false, StatusCodes.Status400BadRequest, ErrorMessageEnum.杖中沒有發現指定使用者ID); return(BadRequest(apiResult)); } UserID = Convert.ToInt32(claimSID); var fooUser = await _context.LobUsers.Include(x => x.Department).FirstOrDefaultAsync(x => x.Id == UserID); if (fooUser == null) { apiResult = APIResultFactory.Build(false, StatusCodes.Status404NotFound, ErrorMessageEnum.沒有發現指定的該使用者資料); return(NotFound(apiResult)); } List <NotificationTokenResponseDTO> NotificationTokenResponseDTOs = new List <NotificationTokenResponseDTO>(); var fooList = await _context.NotificationTokens.Include(x => x.User) .Where(x => x.User.Id == fooUser.Id).OrderByDescending(x => x.RegistrationTime).Take(100).ToListAsync(); foreach (var item in fooList) { NotificationTokenResponseDTO fooObject = new NotificationTokenResponseDTO() { Id = item.Id, User = new UserDTO() { Id = item.User.Id }, Invalid = item.Invalid, OSType = (OSTypeDTO)Enum.Parse(typeof(OSTypeDTO), item.OSType.ToString()), RegistrationTime = item.RegistrationTime, Token = item.Token, }; NotificationTokenResponseDTOs.Add(fooObject); } apiResult = APIResultFactory.Build(true, StatusCodes.Status200OK, ErrorMessageEnum.None, payload: NotificationTokenResponseDTOs); return(Ok(apiResult)); }
public async Task <IActionResult> GetExceptionRecords() { var claimSID = User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value; if (claimSID == null) { apiResult = APIResultFactory.Build(false, StatusCodes.Status400BadRequest, ErrorMessageEnum.杖中沒有發現指定使用者ID); return(BadRequest(apiResult)); } UserID = Convert.ToInt32(claimSID); var fooUser = await _context.LobUsers.Include(x => x.Department).FirstOrDefaultAsync(x => x.Id == UserID); if (fooUser == null) { apiResult = APIResultFactory.Build(false, StatusCodes.Status404NotFound, ErrorMessageEnum.沒有發現指定的該使用者資料); return(NotFound(apiResult)); } var fooList = await _context.ExceptionRecords.Include(x => x.User) .Where(x => x.User.Id == fooUser.Id).OrderByDescending(x => x.ExceptionTime).Take(100).ToListAsync(); List <ExceptionRecordResponseDTO> ExceptionRecordResponseDTOs = new List <ExceptionRecordResponseDTO>(); foreach (var item in fooList) { ExceptionRecordResponseDTO fooNode = new ExceptionRecordResponseDTO() { Id = item.Id, CallStack = item.CallStack, DeviceModel = item.DeviceModel, DeviceName = item.DeviceName, ExceptionTime = item.ExceptionTime, Message = item.Message, OSType = item.OSType, OSVersion = item.OSVersion, }; ExceptionRecordResponseDTOs.Add(fooNode); } apiResult = APIResultFactory.Build(true, StatusCodes.Status200OK, ErrorMessageEnum.None, payload: ExceptionRecordResponseDTOs); return(Ok(apiResult)); }
public async Task <IActionResult> Get([FromRoute] int id) { APIResult apiResult; var record = await OrderService.GetAsync(id); var result = mapper.Map <OrderMasterDto>(record); if (record != null && record.Id != 0) { apiResult = APIResultFactory.Build(true, StatusCodes.Status200OK, ErrorMessageEnum.None, payload: result); } else { apiResult = APIResultFactory.Build(false, StatusCodes.Status200OK, ErrorMessageEnum.沒有任何符合資料存在, payload: result); } return(Ok(apiResult)); }
public async Task <IActionResult> Get() { var claimSID = User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value; if (claimSID == null) { apiResult = APIResultFactory.Build(false, StatusCodes.Status400BadRequest, ErrorMessageEnum.杖中沒有發現指定使用者ID); return(BadRequest(apiResult)); } UserID = Convert.ToInt32(claimSID); var fooUser = await _context.LobUsers.Include(x => x.Department).FirstOrDefaultAsync(x => x.Id == UserID); if (fooUser == null) { apiResult = APIResultFactory.Build(false, StatusCodes.Status404NotFound, ErrorMessageEnum.沒有發現指定的該使用者資料); return(NotFound(apiResult)); } List <InvoiceResponseDTO> fooInvoiceResponseDTO = new List <InvoiceResponseDTO>(); foreach (var item in _context.Invoices .Include(x => x.User).ThenInclude(x => x.Department) .Where(x => x.User.Id == UserID)) { InvoiceResponseDTO fooObject = new InvoiceResponseDTO() { Id = item.Id, InvoiceNo = item.InvoiceNo, Date = item.Date, Memo = item.Memo, user = new UserDTO() { Id = item.User.Id } }; fooInvoiceResponseDTO.Add(fooObject); } apiResult = APIResultFactory.Build(true, StatusCodes.Status200OK, ErrorMessageEnum.None, payload: fooInvoiceResponseDTO); return(Ok(apiResult)); }
public async Task Invoke(HttpContext httpContext) { try { var ModelState = httpContext.Features; await _next(httpContext); } catch (Exception ex) { httpContext.Response.Clear(); httpContext.Response.StatusCode = StatusCodes.Status500InternalServerError; httpContext.Response.ContentType = "application/json"; APIResult apiResult = APIResultFactory.Build(false, StatusCodes.Status500InternalServerError, Helpers.ErrorMessageEnum.Exception, exceptionMessage: $"({ex.GetType().Name}), {ex.Message}{Environment.NewLine}{ex.StackTrace}"); await httpContext.Response.WriteAsync(JsonConvert.SerializeObject(apiResult)); } }
public async Task <IActionResult> Delete([FromRoute] int id) { APIResult apiResult; var record = await LeaveFormService.GetAsync(id); var result = mapper.Map <LeaveFormDto>(record); if (record != null) { #region 刪除記錄前的紀錄完整性檢查 VerifyRecordResult verify = await LeaveFormService.BeforeDeleteCheckAsync(record); if (verify.Success == false) { apiResult = APIResultFactory.Build(false, StatusCodes.Status200OK, ErrorMessageMappingHelper.Instance.GetErrorMessage(verify.MessageId), payload: result); return(Ok(apiResult)); } #endregion var verifyRecordResult = await LeaveFormService.DeleteAsync(id); if (verifyRecordResult.Success) { apiResult = APIResultFactory.Build(true, StatusCodes.Status202Accepted, ErrorMessageEnum.None, payload: null); } else { apiResult = APIResultFactory.Build(false, StatusCodes.Status200OK, ErrorMessageEnum.無法刪除紀錄, payload: result); } } else { apiResult = APIResultFactory.Build(false, StatusCodes.Status200OK, ErrorMessageEnum.沒有任何符合資料存在, payload: result); } return(Ok(apiResult)); }
public async Task <IActionResult> GetByMaster([FromRoute] int id) { APIResult apiResult; #region 建立查詢物件 DataRequest dataRequest = new DataRequest() { Skip = 0, Take = 0, Search = "", Sorted = null, }; #endregion var records = await WorkingLogDetailService.GetByHeaderIDAsync(id, dataRequest); var result = mapper.Map <List <WorkingLogDetailDto> >(records.Result); apiResult = APIResultFactory.Build(true, StatusCodes.Status200OK, ErrorMessageEnum.None, payload: result); return(Ok(apiResult)); }
public async Task <IActionResult> Put([FromRoute] int id, [FromBody] CRUDDto data) { APIResult apiResult; #region 驗證 DTO 物件的資料一致性 if (!ModelState.IsValid) { apiResult = APIResultFactory.Build(false, StatusCodes.Status200OK, "傳送過來的資料有問題", payload: null); return(Ok(apiResult)); } #endregion #region 更新物件 await Task.Yield(); CRUDDto CRUDDto = data; #endregion apiResult = APIResultFactory.Build(true, StatusCodes.Status202Accepted, ErrorMessageEnum.None, payload: null); return(Ok(apiResult)); }
public async Task <IActionResult> Post([FromBody] CRUDDto data) { APIResult apiResult; #region 驗證 DTO 物件的資料一致性 if (!ModelState.IsValid) { apiResult = APIResultFactory.Build(false, StatusCodes.Status200OK, "傳送過來的資料有問題", payload: null); return(Ok(apiResult)); } #endregion #region 新增紀錄 await Task.Yield(); data.Id = new Random().Next(1, 99999); #endregion apiResult = APIResultFactory.Build(false, StatusCodes.Status200OK, ErrorMessageEnum.None, payload: data); return(Ok(apiResult)); }
public async Task <IActionResult> GetCommUserGroupItems(CommUserGroupItemRequestDTO CommUserGroupItemRequestDTO) { List <CommUserGroupItemResponseDTO> CommUserGroupItemResponseDTO = new List <CommUserGroupItemResponseDTO>(); foreach (var item in await _context.CommUserGroupItems.Include(x => x.CommUserGroup).Where(x => x.CommUserGroup.Id == CommUserGroupItemRequestDTO.Id).ToListAsync()) { CommUserGroupItemResponseDTO fooObject = new CommUserGroupItemResponseDTO() { Id = item.Id, Name = item.Name, Email = item.Email, Mobile = item.Mobile, Phone = item.Phone, }; CommUserGroupItemResponseDTO.Add(fooObject); } APIResult apiResult = APIResultFactory.Build(true, StatusCodes.Status200OK, ErrorMessageEnum.None, payload: CommUserGroupItemResponseDTO); return(Ok(apiResult)); }
public async Task <IActionResult> Get() { APIResult apiResult; #region 建立查詢物件 DataRequest dataRequest = new DataRequest() { Skip = 0, Take = 0, Search = "", Sorted = null, }; #endregion var records = await OrderService.GetAsync(dataRequest); var result = mapper.Map <List <OrderMasterDto> >(records.Result); apiResult = APIResultFactory.Build(true, StatusCodes.Status200OK, ErrorMessageEnum.None, payload: result); return(Ok(apiResult)); }
public async Task <IActionResult> Post(LoginRequestDto loginRequestDTO) { APIResult apiResult; await Task.Yield(); if (ModelState.IsValid == false) { apiResult = APIResultFactory.Build(false, StatusCodes.Status200OK, ErrorMessageEnum.傳送過來的資料有問題); return(Ok(apiResult)); } (MyUserAdapterModel user, string message) = await myUserService.CheckUser(loginRequestDTO.Account, loginRequestDTO.Password); if (user == null) { apiResult = APIResultFactory.Build(false, StatusCodes.Status400BadRequest, ErrorMessageEnum.帳號或密碼不正確); return(BadRequest(apiResult)); } string token = GenerateToken(user); string refreshToken = GenerateRefreshToken(user); LoginResponseDto LoginResponseDTO = new LoginResponseDto() { Account = loginRequestDTO.Account, Id = user.Id, Name = loginRequestDTO.Account, Token = token, TokenExpireMinutes = tokenConfiguration.JwtExpireMinutes, RefreshToken = refreshToken, RefreshTokenExpireDays = tokenConfiguration.JwtRefreshExpireDays, }; apiResult = APIResultFactory.Build(true, StatusCodes.Status200OK, ErrorMessageEnum.None, payload: LoginResponseDTO); return(Ok(apiResult)); }
public override void OnActionExecuting(ActionExecutingContext context) { if (!context.ModelState.IsValid) { string fooErrors = ""; if (context.ModelState.ErrorCount > 0) { StringBuilder sb = new StringBuilder(); var fooConnectChart = $"{Environment.NewLine}"; foreach (var item in context.ModelState) { sb.Append(fooConnectChart); sb.Append($"{item.Key} : "); var fooErrorConnectChart = $" "; foreach (var errorItem in item.Value.Errors) { sb.Append(fooErrorConnectChart); if (errorItem.ErrorMessage != null) { sb.Append($"{errorItem.ErrorMessage}"); } else { sb.Append($"{errorItem.Exception.Message}"); } fooErrorConnectChart = ", "; } fooConnectChart = ", "; } fooErrors = sb.ToString(); } var apiResult = APIResultFactory.Build(false, StatusCodes.Status400BadRequest, ErrorMessageEnum.傳送過來的資料有問題, exceptionMessage: fooErrors, replaceExceptionMessage: false); context.Result = new BadRequestObjectResult(apiResult); } }
public async Task <IActionResult> Post(LoginRequestDTO loginRequestDTO) { var fooUser = await context.LobUsers.Include(x => x.Department).FirstOrDefaultAsync(x => x.Account == loginRequestDTO.Account && x.Password == loginRequestDTO.Password); if (fooUser == null) { APIResult apiResult = APIResultFactory.Build(false, StatusCodes.Status400BadRequest, ErrorMessageEnum.帳號或密碼不正確); return(BadRequest(apiResult)); } else { string token = GenerateToken(fooUser); string refreshToken = GenerateRefreshToken(fooUser); LoginResponseDTO LoginResponseDTO = fooUser.ToLoginResponseDTO( token, refreshToken, configuration["Tokens:JwtExpireMinutes"], configuration["Tokens:JwtRefreshExpireDays"]); APIResult apiResult = APIResultFactory.Build(true, StatusCodes.Status200OK, ErrorMessageEnum.None, payload: LoginResponseDTO); return(Ok(apiResult)); } }
public async Task <IActionResult> RefreshToken() { APIResult apiResult; await Task.Yield(); LoginRequestDto loginRequestDTO = new LoginRequestDto() { Account = User.FindFirst(ClaimTypes.Sid)?.Value, }; MyUserAdapterModel user = await myUserService.GetAsync(Convert.ToInt32(loginRequestDTO.Account)); if (user.Id == 0) { apiResult = APIResultFactory.Build(false, StatusCodes.Status401Unauthorized, ErrorMessageEnum.沒有發現指定的該使用者資料); return(BadRequest(apiResult)); } string token = GenerateToken(user); string refreshToken = GenerateRefreshToken(user); LoginResponseDto LoginResponseDTO = new LoginResponseDto() { Account = loginRequestDTO.Account, Id = 0, Name = loginRequestDTO.Account, Token = token, TokenExpireMinutes = tokenConfiguration.JwtExpireMinutes, RefreshToken = refreshToken, RefreshTokenExpireDays = tokenConfiguration.JwtRefreshExpireDays, }; apiResult = APIResultFactory.Build(true, StatusCodes.Status200OK, ErrorMessageEnum.None, payload: LoginResponseDTO); return(Ok(apiResult)); }
public async Task <IActionResult> GetSystemEnvironment() { SystemEnvironmentResponseDTO SystemEnvironmentResponseDTO = new SystemEnvironmentResponseDTO(); var fooObject = await _context.SystemEnvironment.FirstAsync(); if (fooObject != null) { SystemEnvironmentResponseDTO.Id = fooObject.Id; SystemEnvironmentResponseDTO.AppName = fooObject.AppName; SystemEnvironmentResponseDTO.AndroidVersion = fooObject.AndroidVersion; SystemEnvironmentResponseDTO.AndroidUrl = fooObject.AndroidUrl; SystemEnvironmentResponseDTO.iOSVersion = fooObject.iOSVersion; SystemEnvironmentResponseDTO.iOSUrl = fooObject.iOSUrl; APIResult apiResult = APIResultFactory.Build(true, StatusCodes.Status200OK, ErrorMessageEnum.None, payload: SystemEnvironmentResponseDTO); return(Ok(apiResult)); } else { APIResult apiResult = APIResultFactory.Build(false, StatusCodes.Status404NotFound, ErrorMessageEnum.沒有任何符合資料存在); return(NotFound(apiResult)); } }
public async Task <IActionResult> Post([FromBody] OrderMasterDto data) { APIResult apiResult; #region 驗證 DTO 物件的資料一致性 if (!ModelState.IsValid) { apiResult = APIResultFactory.Build(false, StatusCodes.Status200OK, ErrorMessageEnum.傳送過來的資料有問題, payload: data); return(Ok(apiResult)); } #endregion OrderMasterAdapterModel record = mapper.Map <OrderMasterAdapterModel>(data); if (record != null) { var result = mapper.Map <OrderMasterDto>(record); #region 新增記錄前的紀錄完整性檢查 VerifyRecordResult verify = await OrderService.BeforeAddCheckAsync(record); if (verify.Success == false) { apiResult = APIResultFactory.Build(false, StatusCodes.Status200OK, ErrorMessageMappingHelper.Instance.GetErrorMessage(verify.MessageId), payload: result); return(Ok(apiResult)); } #endregion var verifyRecordResult = await OrderService.AddAsync(record); if (verifyRecordResult.Success) { apiResult = APIResultFactory.Build(true, StatusCodes.Status201Created, ErrorMessageEnum.None, payload: null); } else { if (verifyRecordResult.MessageId == ErrorMessageEnum.客製化文字錯誤訊息) { if (verifyRecordResult.Exception == null) { apiResult = APIResultFactory.Build(false, StatusCodes.Status200OK, verifyRecordResult.Message, payload: result); } else { apiResult = APIResultFactory.Build(false, StatusCodes.Status200OK, verifyRecordResult.Message, payload: result, exceptionMessage: verifyRecordResult.Exception.Message, replaceExceptionMessage: true); } } else { apiResult = APIResultFactory.Build(false, StatusCodes.Status200OK, verifyRecordResult.MessageId, payload: result); } } } else { apiResult = APIResultFactory.Build(false, StatusCodes.Status200OK, ErrorMessageEnum.傳送過來的資料有問題, payload: data); } return(Ok(apiResult)); }
public async Task <IActionResult> Put([FromRoute] int id, [FromBody] InvoiceDetailRequestDTO invoiceDetailRequestDTO) { var claimSID = User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value; if (claimSID == null) { apiResult = APIResultFactory.Build(false, StatusCodes.Status400BadRequest, ErrorMessageEnum.杖中沒有發現指定使用者ID); return(BadRequest(apiResult)); } UserID = Convert.ToInt32(claimSID); var fooUser = await _context.LobUsers.Include(x => x.Department).FirstOrDefaultAsync(x => x.Id == UserID); if (fooUser == null) { apiResult = APIResultFactory.Build(false, StatusCodes.Status404NotFound, ErrorMessageEnum.沒有發現指定的該使用者資料); return(NotFound(apiResult)); } if (!ModelState.IsValid) { apiResult = APIResultFactory.Build(false, StatusCodes.Status400BadRequest, ErrorMessageEnum.傳送過來的資料有問題, exceptionMessage: $"傳送過來的資料有問題 {ModelState}"); return(BadRequest(apiResult)); } if (invoiceDetailRequestDTO.Id != id) { apiResult = APIResultFactory.Build(false, StatusCodes.Status400BadRequest, ErrorMessageEnum.紀錄更新所指定ID不一致); return(BadRequest(apiResult)); } var invoiceOnDB = await _context.Invoices .FirstOrDefaultAsync(x => x.Id == invoiceDetailRequestDTO.Invoice.Id); if (invoiceOnDB == null) { apiResult = APIResultFactory.Build(false, StatusCodes.Status404NotFound, ErrorMessageEnum.沒有發現指定的發票); return(NotFound(apiResult)); } var invoiceDetailOnDB = await _context.InvoiceDetails .Include(x => x.Invoice) .FirstOrDefaultAsync(x => x.Id == invoiceDetailRequestDTO.Id); if (invoiceDetailOnDB == null) { apiResult = APIResultFactory.Build(false, StatusCodes.Status404NotFound, ErrorMessageEnum.沒有發現指定的發票明細項目); return(NotFound(apiResult)); } invoiceDetailOnDB.Cnt = invoiceDetailRequestDTO.Cnt; invoiceDetailOnDB.Flag = invoiceDetailRequestDTO.Flag; invoiceDetailOnDB.Memo = invoiceDetailRequestDTO.Memo; invoiceDetailOnDB.PictureName = invoiceDetailRequestDTO.PictureName; invoiceDetailOnDB.Price = invoiceDetailRequestDTO.Price; invoiceDetailOnDB.Qty = invoiceDetailRequestDTO.Qty; invoiceDetailOnDB.SubTotal = invoiceDetailRequestDTO.SubTotal; invoiceDetailOnDB.TDate = invoiceDetailRequestDTO.TDate; _context.Entry(invoiceOnDB).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!await InvoiceDetailExists(invoiceDetailRequestDTO.Id)) { apiResult = APIResultFactory.Build(false, StatusCodes.Status409Conflict, ErrorMessageEnum.要更新的紀錄_發生同時存取衝突_已經不存在資料庫上); return(Conflict(apiResult)); } else { apiResult = APIResultFactory.Build(false, StatusCodes.Status409Conflict, ErrorMessageEnum.紀錄更新時_發生同時存取衝突); return(Conflict(apiResult)); } } catch (Exception ex) { apiResult = APIResultFactory.Build(false, StatusCodes.Status500InternalServerError, Helpers.ErrorMessageEnum.Exception, exceptionMessage: $"({ex.GetType().Name}), {ex.Message}{Environment.NewLine}{ex.StackTrace}"); return(StatusCode(StatusCodes.Status500InternalServerError, apiResult)); } apiResult = APIResultFactory.Build(true, StatusCodes.Status202Accepted, ErrorMessageEnum.None, payload: invoiceDetailRequestDTO); return(Accepted(apiResult)); }
public async Task <IActionResult> Get([FromRoute] int invoiceid) { var claimSID = User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value; if (claimSID == null) { apiResult = APIResultFactory.Build(false, StatusCodes.Status400BadRequest, ErrorMessageEnum.杖中沒有發現指定使用者ID); return(BadRequest(apiResult)); } UserID = Convert.ToInt32(claimSID); var fooUser = await _context.LobUsers.Include(x => x.Department).FirstOrDefaultAsync(x => x.Id == UserID); if (fooUser == null) { apiResult = APIResultFactory.Build(false, StatusCodes.Status404NotFound, ErrorMessageEnum.沒有發現指定的該使用者資料); return(NotFound(apiResult)); } if (!ModelState.IsValid) { apiResult = APIResultFactory.Build(false, StatusCodes.Status400BadRequest, ErrorMessageEnum.傳送過來的資料有問題, exceptionMessage: $"傳送過來的資料有問題 {ModelState}"); return(BadRequest(apiResult)); } var invoiceOnDB = await _context.Invoices .Include(x => x.Details) .FirstOrDefaultAsync(x => x.Id == invoiceid); if (invoiceOnDB == null) { apiResult = APIResultFactory.Build(false, StatusCodes.Status404NotFound, ErrorMessageEnum.沒有發現指定的發票); return(NotFound(apiResult)); } List <InvoiceDetailResponseDTO> fooInvoiceDetailResponseDTO = new List <InvoiceDetailResponseDTO>(); foreach (var item in invoiceOnDB.Details) { InvoiceDetailResponseDTO fooObject = new InvoiceDetailResponseDTO() { Id = item.Id, Invoice = new InvoiceDTO() { Id = item.Invoice.Id }, Cnt = item.Cnt, Flag = item.Flag, Memo = item.Memo, PictureName = item.PictureName, Price = item.Price, Qty = item.Qty, SubTotal = item.SubTotal, TDate = item.TDate, }; fooInvoiceDetailResponseDTO.Add(fooObject); } apiResult = APIResultFactory.Build(true, StatusCodes.Status200OK, ErrorMessageEnum.None, payload: fooInvoiceDetailResponseDTO); return(Ok(apiResult)); }
public async Task <IActionResult> Put([FromRoute] int id, [FromBody] ProductDto data) { APIResult apiResult; #region 驗證 DTO 物件的資料一致性 if (!ModelState.IsValid) { apiResult = APIResultFactory.Build(false, StatusCodes.Status200OK, ErrorMessageEnum.傳送過來的資料有問題, payload: data); return(Ok(apiResult)); } #endregion var record = await ProductService.GetAsync(id); if (record != null && record.Id != 0) { ProductAdapterModel recordTarget = mapper.Map <ProductAdapterModel>(data); recordTarget.Id = id; var result = mapper.Map <ProductDto>(recordTarget); #region 修改記錄前的紀錄完整性檢查 VerifyRecordResult verify = await ProductService.BeforeUpdateCheckAsync(record); if (verify.Success == false) { apiResult = APIResultFactory.Build(false, StatusCodes.Status200OK, ErrorMessageMappingHelper.Instance.GetErrorMessage(verify.MessageId), payload: result); return(Ok(apiResult)); } #endregion var verifyRecordResult = await ProductService.UpdateAsync(recordTarget); if (verifyRecordResult.Success) { apiResult = APIResultFactory.Build(true, StatusCodes.Status202Accepted, ErrorMessageEnum.None, payload: null); } else { if (verifyRecordResult.MessageId == ErrorMessageEnum.客製化文字錯誤訊息) { if (verifyRecordResult.Exception == null) { apiResult = APIResultFactory.Build(false, StatusCodes.Status200OK, verifyRecordResult.Message, payload: result); } else { apiResult = APIResultFactory.Build(false, StatusCodes.Status200OK, verifyRecordResult.Message, payload: result, exceptionMessage: verifyRecordResult.Exception.Message, replaceExceptionMessage: true); } } else { apiResult = APIResultFactory.Build(false, StatusCodes.Status200OK, verifyRecordResult.MessageId, payload: result); } } } else { apiResult = APIResultFactory.Build(false, StatusCodes.Status200OK, ErrorMessageEnum.沒有任何符合資料存在, payload: data); } return(Ok(apiResult)); }
public async Task <IActionResult> PutLeaveForm([FromRoute] int id, [FromBody] LeaveFormRequestDTO leaveForm) { var claimSID = User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value; if (claimSID == null) { apiResult = APIResultFactory.Build(false, StatusCodes.Status400BadRequest, ErrorMessageEnum.杖中沒有發現指定使用者ID); return(BadRequest(apiResult)); } UserID = Convert.ToInt32(claimSID); var fooUser = await _context.LobUsers.Include(x => x.Department).FirstOrDefaultAsync(x => x.Id == UserID); if (fooUser == null) { apiResult = APIResultFactory.Build(false, StatusCodes.Status404NotFound, ErrorMessageEnum.沒有發現指定的該使用者資料); return(NotFound(apiResult)); } if (!ModelState.IsValid) { apiResult = APIResultFactory.Build(false, StatusCodes.Status400BadRequest, ErrorMessageEnum.傳送過來的資料有問題, exceptionMessage: $"傳送過來的資料有問題 {ModelState}"); return(BadRequest(apiResult)); } if (leaveForm.id != id) { apiResult = APIResultFactory.Build(false, StatusCodes.Status400BadRequest, ErrorMessageEnum.紀錄更新所指定ID不一致); return(BadRequest(apiResult)); } var fooLeaveFormType = await _context.LeaveFormTypes.FindAsync(leaveForm.leaveFormType.Id); if (fooLeaveFormType == null) { apiResult = APIResultFactory.Build(false, StatusCodes.Status404NotFound, ErrorMessageEnum.沒有發現指定的請假單類別); return(NotFound(apiResult)); } var leaveFormOnDB = await _context.LeaveForms.Include(x => x.LeaveFormType) .Include(x => x.User).ThenInclude(x => x.Department) .FirstOrDefaultAsync(x => x.Id == leaveForm.id); if (leaveFormOnDB == null) { apiResult = APIResultFactory.Build(false, StatusCodes.Status404NotFound, ErrorMessageEnum.沒有發現指定的請假單); return(NotFound(apiResult)); } else if (leaveFormOnDB.User.Id != UserID) { apiResult = APIResultFactory.Build(false, StatusCodes.Status400BadRequest, ErrorMessageEnum.杖Token上標示的使用者與傳送過來的使用者不一致); return(BadRequest(apiResult)); } leaveFormOnDB.BeginTime = leaveForm.BeginTime; leaveFormOnDB.Description = leaveForm.Description; leaveFormOnDB.EndTime = leaveForm.EndTime; leaveFormOnDB.TotalHours = leaveForm.TotalHours; leaveFormOnDB.LeaveFormType = fooLeaveFormType; _context.Entry(leaveFormOnDB).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!await LeaveFormExists(leaveForm.id)) { apiResult = APIResultFactory.Build(false, StatusCodes.Status409Conflict, ErrorMessageEnum.要更新的紀錄_發生同時存取衝突_已經不存在資料庫上); return(Conflict(apiResult)); } else { apiResult = APIResultFactory.Build(false, StatusCodes.Status409Conflict, ErrorMessageEnum.紀錄更新時_發生同時存取衝突); return(Conflict(apiResult)); } } catch (Exception ex) { apiResult = APIResultFactory.Build(false, StatusCodes.Status500InternalServerError, Helpers.ErrorMessageEnum.Exception, exceptionMessage: $"({ex.GetType().Name}), {ex.Message}{Environment.NewLine}{ex.StackTrace}"); return(StatusCode(StatusCodes.Status500InternalServerError, apiResult)); } apiResult = APIResultFactory.Build(true, StatusCodes.Status202Accepted, ErrorMessageEnum.None, payload: leaveForm); return(Accepted(apiResult)); }