public async Task <IActionResult> Post(int sheetYear, int sheetMonth, [FromBody] SheetEntryDTO value)
        {
            Sheet targetSheet = await this._sheetRetrievalService.GetBySubjectAsync(sheetMonth, sheetYear, this.OwnerId).EnsureNotNull();

            this.EntityOwnerService.EnsureOwner(targetSheet, this.OwnerId);

            if (!this.ModelState.IsValid)
            {
                return(this.BadRequest(this.ModelState));
            }

            SheetEntry entry = this._mappingEngine.Map <SheetEntryDTO, SheetEntry>(value);

            entry.Sheet                 = targetSheet;
            entry.CreateTimestamp       = DateTime.Now;
            entry.UpdateTimestamp       = entry.CreateTimestamp;
            entry.Sheet.UpdateTimestamp = DateTime.Now;
            entry.SortOrder             = targetSheet.Entries.Max(x => (int?)x.SortOrder).GetValueOrDefault() + 1;

            this._sheetEntryRepository.Add(entry);
            await this._sheetEntryRepository.SaveChangesAsync();

            await this._sheetLastVisitedMarkerService.AddOrUpdateImmediateAsync(targetSheet, this.User.Identity.GetUserId());

            return(this.CreatedAtRoute("SheetEntry-Get", new { id = entry.Id }, await this.Get(entry.Id, sheetYear, sheetMonth)));
        }
        private async Task <SheetEntry> GetByIdAsync(int id)
        {
            SheetEntry entry = await this._sheetEntryRepository.GetAll().Include(x => x.Sheet).Include(x => x.Sheet.Owner).Include(x => x.Category).Include(x => x.Tags).FirstOrDefaultAsync(x => x.Id == id).EnsureNotNull();

            this.EnsureCorrectSheet(entry);
            this.EntityOwnerService.EnsureOwner(entry.Sheet, this.OwnerId);
            return(entry);
        }
        private void EnsureCorrectSheet(SheetEntry sheetEntry)
        {
            RouteData routeData = this.RouteData;

            int sheetMonth = Convert.ToInt32(routeData.Values["sheetMonth"]);
            int sheetYear  = Convert.ToInt32(routeData.Values["sheetYear"]);

            if (sheetEntry.Sheet.Subject.Month != sheetMonth || sheetEntry.Sheet.Subject.Year != sheetYear)
            {
                throw new HttpStatusException(HttpStatusCode.BadRequest);
            }
        }
        public async Task <IActionResult> Delete(int id)
        {
            SheetEntry entry = await this.GetByIdAsync(id);

            entry.Sheet.UpdateTimestamp = DateTime.Now;

            await this._sheetLastVisitedMarkerService.AddOrUpdateImmediateAsync(entry.Sheet, this.User.Identity.GetUserId());

            this._sheetEntryRepository.Delete(entry);
            await this._sheetEntryRepository.SaveChangesAsync();

            return(this.NoContent());
        }
        public async Task <SheetEntryDTO> Get(int id, int sheetYear, int sheetMonth)
        {
            Sheet targetSheet = await this._sheetRetrievalService.GetBySubjectAsync(sheetMonth, sheetYear, this.OwnerId).EnsureNotNull();

            this.EntityOwnerService.EnsureOwner(targetSheet, this.OwnerId);

            SheetEntry entry = await this._sheetEntryRepository.FindByIdAsync(id).EnsureNotNull();

            this.EnsureCorrectSheet(entry);

            PreviousSheetVisitMarker marker = await this._sheetLastVisitedMarkerService.GetAndUpdateAsync(targetSheet, this.User.Identity.GetUserId());

            return(this._mappingEngine.Map <SheetEntry, SheetEntryDTO>(entry, opts => opts.SetPreviousSheetVisitMarker(marker)));
        }
        public async Task <IActionResult> MutateOrder(int id, SortOrderMutationType mutation)
        {
            int delta = (int)mutation;

            SheetEntry entry = await this.GetByIdAsync(id);

            Trace.Assert(entry.Category != null);

            entry.Sheet.UpdateTimestamp = DateTime.Now;
            entry.SortOrder            += delta;

            this._sheetEntryRepository.ReplaceSortOrder(entry.Sheet, entry.SortOrder, entry.SortOrder - delta);
            await this._sheetEntryRepository.SaveChangesAsync();

            return(this.NoContent());
        }
        public async Task <IActionResult> Put(int id, [FromBody] SheetEntryDTO value)
        {
            SheetEntry entry = await this.GetByIdAsync(id);

            if (!this.ModelState.IsValid)
            {
                return(this.BadRequest(this.ModelState));
            }

            this._mappingEngine.Map(value, entry);
            entry.UpdateTimestamp       = DateTime.Now;
            entry.Sheet.UpdateTimestamp = DateTime.Now;

            await this._sheetEntryRepository.SaveChangesAsync();

            await this._sheetLastVisitedMarkerService.AddOrUpdateImmediateAsync(entry.Sheet, this.User.Identity.GetUserId());

            return(this.NoContent());
        }