public Book Add(Book item) { ChangePriority(item); item.Id = nextId; books.Add(item); nextId++; return item; }
// PUT api/listtoread/5 public void Put(Book item) { if (ModelState.IsValid) { item.Priority = GetPriority(item); repository.Update(item); } }
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); }
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; }
public Book Post(Book item) { if (ModelState.IsValid) { item.Priority = GetPriority(item); return repository.Add(item); } else { Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState); } return null; }
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); } }
protected void UpdateWithoutChanhgePriority(Book item) { var removed = books.Find(x => x.Id == item.Id); if (removed != null) { Delete(item.Id); } books.Add(item); }