Esempio n. 1
0
        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));
        }
Esempio n. 2
0
        private async Task LoadTimeEntry()
        {
            var url   = $"time-entries/{Id}";
            var model = await ApiService.GetAsync <TimeEntryModel>(url);

            timeEntry = new TimeEntryInputModel
            {
                UserId      = model.UserId,
                ProjectId   = model.ProjectId,
                EntryDate   = model.EntryDate,
                Hours       = model.Hours,
                Description = model.Description
            };

            projectId = model.ProjectId.ToString();
            year      = model.EntryDate.Year.ToString();
            month     = model.EntryDate.Month.ToString();
            day       = model.EntryDate.Day.ToString();
        }
Esempio n. 3
0
        protected override async Task OnInitializedAsync()
        {
            await LoadProjects();

            if (Id > 0)
            {
                await LoadTimeEntry();
            }
            else
            {
                timeEntry = new TimeEntryInputModel
                {
                    UserId    = (await AuthStateProvider.GetUserAsync()).Id,
                    EntryDate = DateTime.Today,
                    Hours     = 1
                };
                year  = DateTime.Today.Year.ToString();
                month = DateTime.Today.Month.ToString();
                day   = DateTime.Today.Day.ToString();
            }
        }
Esempio n. 4
0
        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));
        }