public async Task <ActionResult <TimeEntryModel> > Create(TimeEntryInputModel model) { _logger.LogDebug( $"Creating a new time entry for user {model.UserId}, project {model.ProjectId} and date {model.EntryDate}"); var user = await _dbContext.Users.FindAsync(model.UserId); var project = await _dbContext.Projects .Include(x => x.Client) // Necessary for mapping to TimeEntryModel later .SingleOrDefaultAsync(x => x.Id == model.ProjectId); if (user == null || project == null) { return(NotFound()); } var timeEntry = new TimeEntry { User = user, Project = project, HourRate = user.HourRate }; model.MapTo(timeEntry); await _dbContext.TimeEntries.AddAsync(timeEntry); await _dbContext.SaveChangesAsync(); var resultModel = TimeEntryModel.FromTimeEntry(timeEntry); return(CreatedAtAction(nameof(GetById), "TimeEntries", new { id = timeEntry.Id, version = "2" }, resultModel)); }
public async Task <ActionResult <TimeEntryModel> > GetById(long id) { _logger.LogInformation(message: $"Getting a time entry with id: {id}"); var timeEntry = await _dbContext.TimeEntries.Include(x => x.Project).Include(x => x.Project.Client).Include(x => x.User).SingleOrDefaultAsync(x => x.Id == id); if (timeEntry == null) { return(NotFound()); } return(TimeEntryModel.FromTimeEntry(timeEntry)); }
public async Task <ActionResult <TimeEntryModel> > GetById(long id) { _logger.LogInformation($"Getting a time entry with id {id}"); var timeEntry = await _dbContext.TimeEntries .Include(x => x.Project) .Include(x => x.Project.Client) .Include(x => x.User) .SingleOrDefaultAsync(x => x.Id == id); //Ukljucimo sve da mozemo dobiti ClientName npr u TimeEntry if (timeEntry == null) { return(NotFound()); } return(TimeEntryModel.FromTimeEntry(timeEntry)); //Konvertujemo u TimeEntry i vratimo }
public async Task <ActionResult <TimeEntryModel> > Update(long id, TimeEntryInputModel model) { _logger.LogDebug($"Updating time entry with id {id}"); var timeEntry = await _dbContext.TimeEntries .Include(x => x.User) .Include(x => x.Project) .Include(x => x.Project.Client) .SingleOrDefaultAsync(x => x.Id == id); if (timeEntry == null) { return(NotFound()); } model.MapTo(timeEntry); _dbContext.TimeEntries.Update(timeEntry); await _dbContext.SaveChangesAsync(); return(TimeEntryModel.FromTimeEntry(timeEntry)); }