public async Task <ActionResult <TaskUserWorkingHoursRecord> > CreateRecord([FromBody] CreateWorkingHoursRecordDto creationDto) { if (!Request.Headers.TryGetValue(_requestIdHeaderName, out StringValues requestIdValue)) { return(BadRequest($"{_requestIdHeaderName} header must be specified!")); } string requestId = requestIdValue.ToString(); try { TaskUserWorkingHoursRecord newRecord = _mapper.Map <CreateWorkingHoursRecordDto, TaskUserWorkingHoursRecord>(creationDto); var createdRecord = await _workingHoursService.CreateRecordAsync(newRecord, requestId); return(Ok(createdRecord)); } catch (AlreadyHandledException e) { return(Accepted(e.Message)); } catch (Exception e) { return(BadRequest($"{e.GetType()} : {e.Message}")); } }
public async Task <TaskUserWorkingHoursRecord> UpdateRecordAsync(TaskUserWorkingHoursRecord updatingRecord) { TaskUserWorkingHoursRecord currentRecord = await _workingHoursRepository.GetWorkingHoursRecordByIdAsync(updatingRecord.Id); if (currentRecord.Version != updatingRecord.Version) { throw new VersionsNotMatchException(); } await EnsureTaskUserExistAsync(updatingRecord.TaskId, updatingRecord.UserId); return(await _workingHoursRepository.UpdateWorkingHoursRecordAsync(updatingRecord)); }
public async Task <TaskUserWorkingHoursRecord> AddWorkingHoursRecordAsync(TaskUserWorkingHoursRecord newRecord) { newRecord.Init(); string insertQuery = $"insert into {_tableName} (id, taskid, userid, description, hours, createddate, version) " + $"values('{newRecord.Id}', '{newRecord.TaskId}', '{newRecord.UserId}', '{newRecord.Description}', '{newRecord.Hours}', '{newRecord.CreatedDate}', {newRecord.Version});"; int res = await _connection.ExecuteAsync(insertQuery); if (res <= 0) { throw new DatabaseException("Working hours record create failed"); } return(await GetWorkingHoursRecordByIdAsync(newRecord.Id)); }
public async Task <TaskUserWorkingHoursRecord> UpdateRecordAsync(TaskUserWorkingHoursRecord updatingRecord) { TaskUserWorkingHoursRecord currentRecord = await _workingHoursRepository.GetWorkingHoursRecordByIdAsync(updatingRecord.Id); if (currentRecord == null) { throw new NotFoundException($"Record with id = {updatingRecord.Id} not found"); } if (currentRecord.Version != updatingRecord.Version) { throw new VersionsNotMatchException(); } return(await _workingHoursRepository.UpdateWorkingHoursRecordAsync(updatingRecord)); }
public async Task <TaskUserWorkingHoursRecord> AddWorkingHoursRecordAsync(TaskUserWorkingHoursRecord newRecord) { string id = Guid.NewGuid().ToString(); DateTimeOffset createdDate = DateTimeOffset.UtcNow; int version = 1; string insertQuery = $"insert into {_workingHoursRecordsTableName} (id, taskid, userid, description, hours, createddate, version) " + $"values('{id}', '{newRecord.TaskId}', '{newRecord.UserId}', '{newRecord.Description}', {newRecord.Hours}, '{createdDate}', {version});"; int res = await _connection.ExecuteAsync(insertQuery); if (res <= 0) { throw new DatabaseException("Working hours record create failed"); } return(await GetWorkingHoursRecordByIdAsync(id)); }
public async Task <TaskUserWorkingHoursRecord> CreateRecordAsync(TaskUserWorkingHoursRecord newRecord, string requestId) { if (!(await CheckAndSaveRequestIdAsync(requestId))) { throw new AlreadyHandledException(); } try { return(await _workingHoursRepository.AddWorkingHoursRecordAsync(newRecord)); } catch (Exception) { //rollback request id await _requestsRepository.DeleteRequestIdAsync(requestId); throw; } }
public async Task <TaskUserWorkingHoursRecord> UpdateWorkingHoursRecordAsync(TaskUserWorkingHoursRecord updatedRecord) { int newVersion = updatedRecord.Version + 1; string updateQuery = $"update {_tableName} set " + $"taskid = '{updatedRecord.TaskId}', " + $"description = '{updatedRecord.Description}', " + $"hours = {updatedRecord.Hours}, " + $"version = {newVersion} " + $"where id = '{updatedRecord.Id}';"; int res = await _connection.ExecuteAsync(updateQuery); if (res <= 0) { throw new DatabaseException("Update record failed"); } return(await GetWorkingHoursRecordByIdAsync(updatedRecord.Id)); }
public async Task <ActionResult <MemberWorkingHoursAggregate> > UpdateRecord([FromBody] UpdateWorkingHoursRecordDto updateDto) { try { TaskUserWorkingHoursRecord updatingRecord = _mapper.Map <UpdateWorkingHoursRecordDto, TaskUserWorkingHoursRecord>(updateDto); var updatedRecord = await _workingHoursService.UpdateRecordAsync(updatingRecord); return(Ok(updatedRecord)); } catch (VersionsNotMatchException e) { return(Conflict(e.Message)); } catch (NotFoundException e) { return(NotFound(e.Message)); } catch (Exception e) { return(BadRequest($"{e.GetType()} : {e.Message}")); } }
public async Task <ActionResult <MemberWorkingHoursAggregate> > CreateRecord([FromBody] CreateWorkingHoursRecordDto creationDto) { if (!Request.Headers.TryGetValue(Constants.RequestIdHeaderName, out StringValues requestIdValue)) { return(BadRequest($"{Constants.RequestIdHeaderName} header must be specified!")); } if (!Request.Headers.TryGetValue(Constants.UserIdHeaderName, out StringValues userIdValue)) { return(BadRequest($"{Constants.UserIdHeaderName} header must be specified!")); } string requestId = requestIdValue.ToString(); string userId = userIdValue.ToString(); try { TaskUserWorkingHoursRecord newRecord = _mapper.Map <CreateWorkingHoursRecordDto, TaskUserWorkingHoursRecord>(creationDto); newRecord.UserId = userId; var createdRecord = await _workingHoursService.CreateRecordAsync(newRecord, requestId); return(Ok(createdRecord)); } catch (AlreadyHandledException ahe) { return(Accepted(ahe.Message)); } catch (NotFoundException nfe) { return(NotFound(nfe.Message)); } catch (Exception e) { return(BadRequest($"{e.GetType()} : {e.Message}")); } }