public async Task <IActionResult> ClockIn(TimeSignatureModel body) { try { if (body == null) { IActionResult actionResult = customActionResult.BadRequest("The client set the requested body to null before it was sent."); return(StatusCode(StatusCodes.Status400BadRequest, actionResult)); } else if (string.IsNullOrEmpty(body.typeCode) || string.IsNullOrWhiteSpace(body.typeCode)) { IActionResult actionResult = customActionResult.FieldsRequired( "The 'type code' field has been sent from the client null, empty, or whitespaced." ); return(StatusCode(StatusCodes.Status411LengthRequired, actionResult)); } else if (body.typeCode != "FLX") { IActionResult actionResult = customActionResult.Conflict( "This option is only for 'Fixed Time Signature'. For 'Flexible Time Signature', try the other option." ); return(StatusCode(StatusCodes.Status409Conflict, actionResult)); } else if (string.IsNullOrEmpty(body.wd_typeCode) || string.IsNullOrWhiteSpace(body.wd_typeCode)) { IActionResult actionResult = customActionResult.FieldsRequired( "The Workday 'code type' field has been sent from the client null, empty, or whitespaced." ); return(StatusCode(StatusCodes.Status411LengthRequired, actionResult)); } else { var checkIfAnyWorkdaySavedInDbWithCurrentDate = await CheckIfAnyWorkdaySavedInDbWithCurrentDate(); if ( checkIfAnyWorkdaySavedInDbWithCurrentDate.Count > 0 && checkIfAnyWorkdaySavedInDbWithCurrentDate[0].typeCode == "FLX" && checkIfAnyWorkdaySavedInDbWithCurrentDate[0].stateCode == "TT" && checkIfAnyWorkdaySavedInDbWithCurrentDate[0].endDate.Date == DateTime.Now.Date ) { IActionResult actionResult = customActionResult.Locked( @$ "You already finished your workday for today using flexible time signatures. If you checked in the finish checkbox from the client by mistake, contact your manager. Have a nice day and see you tommorrow" ); return(StatusCode(StatusCodes.Status423Locked, actionResult)); } else { var checkIfAnyTSignatureSavedInDbWithCurrentDate = await CheckIfAnyTSignatureSavedInDbWithCurrentDate(); if (checkIfAnyTSignatureSavedInDbWithCurrentDate.Count > 0) { IActionResult actionResult = customActionResult.Locked( @$ "You already clocked in. You must clock out before you clock in again. Your session already started at {checkIfAnyTSignatureSavedInDbWithCurrentDate[0].startDate}" ); return(StatusCode(StatusCodes.Status423Locked, actionResult)); } else { if (body.wd_typeCode != "ZI") { IActionResult actionResult = customActionResult.BadRequest( @$ "The workday 'code type' value provided by the client is not valid: {body.wd_typeCode}." ); return(StatusCode(StatusCodes.Status400BadRequest, actionResult)); } else { var selectWorkdayById = await SelectWorkdayById(); if (selectWorkdayById.Count < 1) { await InsertIntoWorkday(body); } var selectWorkdayByIdAgain = await SelectWorkdayById(); await ClockIn(body, selectWorkdayByIdAgain); DateTime dateNow = new DateTime(); dateNow = DateTime.Now; IActionResult actionResult = customActionResult.Ok( @$ "You signed successfuly for flexible 'Time Signature' at {dateNow}. Don't forget to clock out." ); return(StatusCode(StatusCodes.Status200OK, actionResult)); } } } } } catch (Exception e) { return(StatusCode(StatusCodes.Status500InternalServerError, e.Message)); } }
public async Task <IActionResult> DeleteAsOrdinaryAsync(int id, TimeSignatureModel body) { try { if (body == null) { IActionResult actionResult = customActionResult.BadRequest( "The client set the requested body to null before it was sent." ); return(StatusCode(StatusCodes.Status400BadRequest, actionResult)); } else { if (string.IsNullOrEmpty(body.typeCode) || string.IsNullOrWhiteSpace(body.typeCode)) { IActionResult actionResult = customActionResult.FieldsRequired( "The 'code type' field has been sent from the client null, empty, or whitespaced." ); return(StatusCode(StatusCodes.Status411LengthRequired, actionResult)); } else if (body.typeCode != "FIX" && _claims.GetUserRole() != "admin") { IActionResult actionResult = customActionResult.Conflict( "This option is only for 'Fixed Time Signature' type. If you want to delete a 'Flexible Time Signature', please contact your manager." ); return(StatusCode(StatusCodes.Status409Conflict, actionResult)); } else if (body.typeCode == "FIX" && _claims.GetUserRole() != "admin") { DynamicParameters selectUserIdByWokrdayId_params = new DynamicParameters(); selectUserIdByWokrdayId_params.Add("@userId", _claims.GetUserId()); selectUserIdByWokrdayId_params.Add("@workdayId", id); string selectUserIdByWokrdayId_string = $@" SELECT [userId] FROM [Workday] WHERE [userId] = @userId AND [id] = @workdayId "; var selectUserIdByWorkdayId_result = await _db.SelectAsync <WorkdayModel, dynamic>(selectUserIdByWokrdayId_string, selectUserIdByWokrdayId_params); if (selectUserIdByWorkdayId_result.Count < 1) { IActionResult actionResult = customActionResult.NotFound("No time signature found by given id"); return(StatusCode(StatusCodes.Status404NotFound, actionResult)); } else if (selectUserIdByWorkdayId_result[0].userId != _claims.GetUserId()) { IActionResult actionResult = customActionResult.Unauthorized( "You are not authorized to delete this time signature." ); return(StatusCode(StatusCodes.Status401Unauthorized, actionResult)); } else { DynamicParameters deleteFixTSignatureByWorkdayId_params = new DynamicParameters(); deleteFixTSignatureByWorkdayId_params.Add("@workdayId", id); string deleteFixTSignaturesByWorkdayId = $@" DELETE FROM [TimeSignature] WHERE [workdayId] = @workdayId AND [typeCode] = 'FIX' "; await _db.DeleteAsync(deleteFixTSignaturesByWorkdayId, deleteFixTSignatureByWorkdayId_params); string deleteFixWorkdayByWorkdayId = $@" DELETE FROM [Workday] WHERE [id] = @workdayId "; await _db.DeleteAsync(deleteFixWorkdayByWorkdayId, deleteFixTSignatureByWorkdayId_params); IActionResult actionResult = customActionResult.Ok( "You deleted successfuly your workday." ); return(StatusCode(StatusCodes.Status200OK, actionResult)); } } else { DynamicParameters selectUserIdByWokrdayId_params = new DynamicParameters(); selectUserIdByWokrdayId_params.Add("@userId", _claims.GetUserId()); selectUserIdByWokrdayId_params.Add("@workdayId", id); string selectUserIdByWokrdayId_string = $@" SELECT [userId] FROM [Workday] WHERE [userId] = @userId AND [id] = @workdayId "; var selectUserIdByWorkdayId_result = await _db.SelectAsync <WorkdayModel, dynamic>(selectUserIdByWokrdayId_string, selectUserIdByWokrdayId_params); if (selectUserIdByWorkdayId_result.Count < 1) { IActionResult actionResult = customActionResult.NotFound("No time signature found by given id"); return(StatusCode(StatusCodes.Status404NotFound, actionResult)); } else if (selectUserIdByWorkdayId_result[0].userId != _claims.GetUserId()) { IActionResult actionResult = customActionResult.Unauthorized( "You are not authorized to delete this time signature." ); return(StatusCode(StatusCodes.Status401Unauthorized, actionResult)); } else { DynamicParameters deleteFixTSignatureByWorkdayId_params = new DynamicParameters(); deleteFixTSignatureByWorkdayId_params.Add("@workdayId", id); string deleteFixTSignaturesByWorkdayId = $@" DELETE FROM [TimeSignature] WHERE [workdayId] = @workdayId "; await _db.DeleteAsync(deleteFixTSignaturesByWorkdayId, deleteFixTSignatureByWorkdayId_params); string deleteFixWorkdayByWorkdayId = $@" DELETE FROM [Workday] WHERE [id] = @workdayId "; await _db.DeleteAsync(deleteFixWorkdayByWorkdayId, deleteFixTSignatureByWorkdayId_params); IActionResult actionResult = customActionResult.Ok( "You deleted successfuly your workday." ); return(StatusCode(StatusCodes.Status200OK, actionResult)); } } } } catch (Exception e) { return(StatusCode(StatusCodes.Status500InternalServerError, e.Message)); } }
public async Task <IActionResult> Register(UserAccountModel body) { try { if (body == null) { IActionResult actionResult = BadRequest( "The client set the requested body to null before it was sent." ); return(StatusCode(StatusCodes.Status400BadRequest, actionResult)); } else { if (string.IsNullOrEmpty(body.firstName) || string.IsNullOrWhiteSpace(body.firstName)) { IActionResult actionResult = customActionResult.FieldsRequired( "The 'firstname' field has been sent from the client null, empty, or whitespaced." ); return(StatusCode(StatusCodes.Status411LengthRequired, actionResult)); } else if (string.IsNullOrEmpty(body.lastName) || string.IsNullOrWhiteSpace(body.lastName)) { IActionResult actionResult = customActionResult.FieldsRequired( "The 'firstname' field has been sent from the client null, empty, or whitespaced." ); return(StatusCode(StatusCodes.Status411LengthRequired, actionResult)); } else if (string.IsNullOrEmpty(body.userName) || string.IsNullOrWhiteSpace(body.userName)) { IActionResult actionResult = customActionResult.FieldsRequired( "The 'email' field has been sent from the client null, empty, or whitespaced." ); return(StatusCode(StatusCodes.Status411LengthRequired, actionResult)); } else if (string.IsNullOrEmpty(body.password) || string.IsNullOrWhiteSpace(body.password)) { IActionResult actionResult = customActionResult.FieldsRequired( "The 'password' field has been sent from the client null, empty, or whitespaced." ); return(StatusCode(StatusCodes.Status411LengthRequired, actionResult)); } else if (string.IsNullOrEmpty(body.role) || string.IsNullOrWhiteSpace(body.role)) { IActionResult actionResult = customActionResult.FieldsRequired( "The 'role' field has been sent from the client null, empty, or whitespaced." ); return(StatusCode(StatusCodes.Status411LengthRequired, actionResult)); } else { DynamicParameters selectParameters = new DynamicParameters(); selectParameters.Add("@userName", body.userName); string selectQuery = @$ "SELECT [id], [userName] FROM [User] WHERE [userName] = @userName"; var resultQuery = await _db.SelectAsync <UserAccountModel, dynamic>(selectQuery, selectParameters); if (resultQuery.Count > 0) { IActionResult actionResult = customActionResult.Conflict( "The user account you are trying to create, already exists." ); return(StatusCode(StatusCodes.Status409Conflict, actionResult)); } else { byte[] salt = hashPassword.CreateSalt(10); string hashedPassword = hashPassword.GenerateSHA256Hash(body.password, salt, false); if (string.IsNullOrEmpty(hashedPassword) || string.IsNullOrWhiteSpace(hashedPassword)) { IActionResult actionResult = customActionResult.Locked( "The password failed to hash, so the account creation process been locked. Please Try Again." ); return(StatusCode(StatusCodes.Status423Locked, actionResult)); } else { DynamicParameters insertParameters = new DynamicParameters(); insertParameters.Add("@firstName", body.firstName); insertParameters.Add("@lastName", body.lastName); insertParameters.Add("@userName", body.userName); insertParameters.Add("@role", body.role); insertParameters.Add("@password", hashedPassword); string insertQuery = @" INSERT INTO [User] ([firstName], [lastName], [userName], [password], [role]) VALUES (@firstName, @lastName, @userName, @password, @role) "; await _db.InsertAsync(insertQuery, insertParameters); IActionResult actionResult = customActionResult.Created( "The user account as been created successfully. " ); return(StatusCode(StatusCodes.Status201Created, actionResult)); } } } } } catch (Exception e) { return(StatusCode(StatusCodes.Status500InternalServerError, e.Message)); } }
public async Task <IActionResult> ClockInAsOrdinary(TimeSignatureModel body) { try { //#region CHECK INPUTS SENT FROM CLIENT if (body == null) { IActionResult actionResult = customActionResult.BadRequest( "The client set the requested body to null before it was sent." ); return(StatusCode(StatusCodes.Status400BadRequest, actionResult)); } else { if (string.IsNullOrEmpty(body.typeCode) || string.IsNullOrWhiteSpace(body.typeCode)) { IActionResult actionResult = customActionResult.FieldsRequired( "The 'code type' field has been sent from the client null, empty, or whitespaced." ); return(StatusCode(StatusCodes.Status411LengthRequired, actionResult)); } else if (body.typeCode != "FIX") { IActionResult actionResult = customActionResult.Conflict( "This option is only for 'Fixed Time Signature'. For 'Flexible Time Signature', try the other option." ); return(StatusCode(StatusCodes.Status409Conflict, actionResult)); } else { var selectTodaysUserTSignatureIdentifiersByUserId = await SelectTodaysUserTSignatureIdentifiersByUserId(body); if (selectTodaysUserTSignatureIdentifiersByUserId.Count < 1) { if (string.IsNullOrEmpty(body.wd_typeCode) || string.IsNullOrWhiteSpace(body.wd_typeCode)) { IActionResult actionResult = customActionResult.FieldsRequired( "The workday 'code type' field has been sent from the client null, empty, or whitespaced." ); return(StatusCode(StatusCodes.Status411LengthRequired, actionResult)); } else if (body.fullDay == false && (body.startDate == null || body.endDate == null)) { IActionResult actionResult = customActionResult.FieldsRequired( "The 'start date' or 'end date' field has been sent from the client null." ); return(StatusCode(StatusCodes.Status411LengthRequired, actionResult)); } else if (body.fullDay == false && (body.startDate != null || body.endDate != null)) { await InsertIntoWorkdayWithOutFullDayOption(body); IActionResult actionResult = customActionResult.Ok(@$ "You signed successfuly without full day option."); return(StatusCode(StatusCodes.Status200OK, actionResult)); } else if (body.fullDay == true && body.startDate == null) { IActionResult actionResult = customActionResult.FieldsRequired("The 'start date' field has been sent from the client null."); return(StatusCode(StatusCodes.Status411LengthRequired, actionResult)); } else if (body.fullDay == true && body.startDate != null) { await InsertIntoWorkdayWithFullDayOption(body); IActionResult actionResult = customActionResult.Ok(@$ "You signed successfuly with full day option."); return(StatusCode(StatusCodes.Status200OK, actionResult)); } else { IActionResult actionResult = customActionResult.NotFound(@$ "No options found with given keys."); return(StatusCode(StatusCodes.Status404NotFound, actionResult)); } } else { var checkDbIfUserClockedInTodayFixTSignature = await CheckDbIfUserClockedInTodayWithFixTSignature(selectTodaysUserTSignatureIdentifiersByUserId); if ( checkDbIfUserClockedInTodayFixTSignature.Count > 0 && selectTodaysUserTSignatureIdentifiersByUserId[0].stateCode == "TT" && selectTodaysUserTSignatureIdentifiersByUserId[0].typeCode == "FIX" ) { if (string.IsNullOrEmpty(body.wd_typeCode) || string.IsNullOrWhiteSpace(body.wd_typeCode)) { IActionResult actionResult = customActionResult.FieldsRequired( "The workday 'code type' field has been sent from the client null, empty, or whitespaced." ); return(StatusCode(StatusCodes.Status411LengthRequired, actionResult)); } else if (body.fullDay == false && (body.startDate == null || body.endDate == null)) { IActionResult actionResult = customActionResult.FieldsRequired( "The 'start date' or 'end date' field has been sent from the client null." ); return(StatusCode(StatusCodes.Status411LengthRequired, actionResult)); } else if (body.fullDay == false && (body.startDate != null || body.endDate != null)) { await InsertIntoWorkdayWithOutFullDayOption(body); IActionResult actionResult = customActionResult.Ok("You signed successfuly without full day option."); return(StatusCode(StatusCodes.Status200OK, actionResult)); } else if (body.fullDay == true && body.startDate == null) { IActionResult actionResult = customActionResult.FieldsRequired( "The 'start date' field has been sent from the client null." ); return(StatusCode(StatusCodes.Status411LengthRequired, actionResult)); } else if (body.fullDay == true && body.startDate != null) { await InsertIntoWorkdayWithFullDayOption(body); IActionResult actionResult = customActionResult.Ok("You signed successfuly with full day option."); return(StatusCode(StatusCodes.Status200OK, actionResult)); } else { IActionResult actionResult = customActionResult.NotFound("No options found with given keys."); return(StatusCode(StatusCodes.Status404NotFound, actionResult)); } } else { IActionResult actionResult = customActionResult.Locked(@$ " You already clocked in for today with workday type code 'ZI'. If you want to, you have the option to update your time signature. Your session started at {selectTodaysUserTSignatureIdentifiersByUserId[0].startDate} and ended at {selectTodaysUserTSignatureIdentifiersByUserId[0].endDate} "); return(StatusCode(StatusCodes.Status423Locked, actionResult)); } } } } } catch (Exception e) { return(StatusCode(StatusCodes.Status500InternalServerError, e.Message)); } }