コード例 #1
0
 public Book Add(Book item)
 {
     ChangePriority(item);
     item.Id = nextId;
     books.Add(item);
     nextId++;
     return item;
 }
コード例 #2
0
 // PUT api/listtoread/5
 public void Put(Book item)
 {
     if (ModelState.IsValid)
     {
         item.Priority = GetPriority(item);
         repository.Update(item);
     }
 }
コード例 #3
0
 public void Update(Book item)
 {
     var removed = books.Find(x => x.Id == item.Id);
     if (removed != null)
     {
         ChangePriority(item);
         Delete(item.Id);
     }
     books.Add(item);
 }
コード例 #4
0
 protected int GetPriority(Book item)
 {
     int lastPriority = repository.GetNextPriority();
     if (!item.Priority.HasValue || item.Priority == 0 || item.Priority > lastPriority)
     {
         if (item.Id != 0)
         {
             return lastPriority - 1;
         }
     }
     return item.Priority.Value;
 }
コード例 #5
0
 public Book Post(Book item)
 {
     if (ModelState.IsValid)
     {
         item.Priority = GetPriority(item);
         return repository.Add(item);
     }
     else
     {
         Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
     }
     return null;
 }
コード例 #6
0
 protected void ChangePriority(Book item)
 {
     if (item.Priority > 0)
     {
         var book = books.Find(x => x.Priority == item.Priority);
         if (book != null)
         {
             if (item.Id != 0)
             {
                 if (book != null)
                 {
                     var oldBook = books.Find(x => x.Id == item.Id);
                     book.Priority = oldBook.Priority;
                 }
             }
             else
             {
                 book.Priority = (books.Count + 1);
             }
             UpdateWithoutChanhgePriority(book);
         }
     }
     else
     {
         item.Priority = ( books.Count + 1);
     }
 }
コード例 #7
0
 protected void UpdateWithoutChanhgePriority(Book item)
 {
     var removed = books.Find(x => x.Id == item.Id);
     if (removed != null)
     {
         Delete(item.Id);
     }
     books.Add(item);
 }