public async Task <ActionResult <TaskGroupDto> > CreateTaskGroupAsync([FromBody] TaskGroupForCreationDto taskGroupForCreation) { var result = await _taskGroupsService.CreateTaskGroupAsync(taskGroupForCreation); if (!result.Success) { return(BadRequest(result.Message)); } return(Ok(result.Data)); }
public async Task <ServiceResponse <TaskGroupDto> > CreateTaskGroupAsync(TaskGroupForCreationDto taskGroupForCreation) { TaskGroup taskGroup = _mapper.Map <TaskGroup>(taskGroupForCreation); try { await _dbContext.TaskGroups.AddAsync(taskGroup); await _dbContext.SaveChangesAsync(); return(new ServiceResponse <TaskGroupDto>(_mapper.Map <TaskGroupDto>(await _dbContext.TaskGroups.FirstOrDefaultAsync(x => x.Id == taskGroup.Id)))); } catch (Exception ex) { return(new ServiceResponse <TaskGroupDto>($"An error occurred when creating the TaskGroup: {ex.Message}")); } }
public async Task <ServiceResponse <TaskGroupDto> > UpdateTaskGroupAsync(int id, TaskGroupForCreationDto taskGroup) { var oldTaskGroup = await _dbContext.TaskGroups.FirstOrDefaultAsync(x => x.Id == id); if (oldTaskGroup == null) { return(new ServiceResponse <TaskGroupDto>("TaskGroup not found.")); } oldTaskGroup.Name = taskGroup.Name; try { _dbContext.TaskGroups.Update(oldTaskGroup); await _dbContext.SaveChangesAsync(); return(new ServiceResponse <TaskGroupDto>(_mapper.Map <TaskGroupDto>(oldTaskGroup))); } catch (Exception ex) { return(new ServiceResponse <TaskGroupDto>($"An error occurred when updating the TaskGroup: {ex.Message}")); } }