public SheetEntryDTO Get(int id)
        {
            SheetEntry entry = this._sheetEntryRepository.FindById(id).EnsureNotNull();

            this.EnsureCorrectSheet(entry);

            return(AutoMapper.Mapper.Map <SheetEntryDTO>(entry));
        }
        private void EnsureCorrectSheet(SheetEntry sheetEntry)
        {
            var routeData  = this.RequestContext.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 HttpResponseException(HttpStatusCode.BadRequest);
            }
        }
        public void Delete(int id)
        {
            SheetEntry entry = this._sheetEntryRepository.FindById(id).EnsureNotNull();

            this.EnsureCorrectSheet(entry);
            this.EntityOwnerService.EnsureOwner(entry.Sheet, this.OwnerId);
            entry.Sheet.UpdateTimestamp = DateTime.Now;

            this._sheetEntryRepository.Delete(entry);
            this._sheetEntryRepository.SaveChanges();
        }
        public InsertId Put(int id, [FromBody] SheetEntryDTO value)
        {
            SheetEntry entry = this._sheetEntryRepository.FindById(id).EnsureNotNull();

            this.EnsureCorrectSheet(entry);
            this.EntityOwnerService.EnsureOwner(entry.Sheet, this.OwnerId);

            AutoMapper.Mapper.Map(value, entry);
            entry.UpdateTimestamp       = DateTime.Now;
            entry.Sheet.UpdateTimestamp = DateTime.Now;

            this._sheetEntryRepository.SaveChanges();

            return(entry.Id);
        }
        public void MutateOrder(int id, SortOrderMutationType mutation)
        {
            int delta = (int)mutation;

            SheetEntry entry = this._sheetEntryRepository.FindById(id).EnsureNotNull();

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

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

            this._sheetEntryRepository.ReplaceSortOrder(entry.Sheet, entry.SortOrder, entry.SortOrder - delta);
            this._sheetEntryRepository.SaveChanges();
        }
        public InsertId Post(int sheetYear, int sheetMonth, [FromBody] SheetEntryDTO value)
        {
            Sheet targetSheet = this._sheetRetrievalService.GetBySubject(sheetMonth, sheetYear, this.OwnerId).EnsureNotNull();

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

            SheetEntry entry = AutoMapper.Mapper.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);
            this._sheetEntryRepository.SaveChanges();

            return(entry.Id);
        }
        private void EnsureCorrectSheet(SheetEntry sheetEntry)
        {
            var routeData = this.RequestContext.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 HttpResponseException(HttpStatusCode.BadRequest);
            }
        }