Example #1
0
        // PUT api/Book/5
        public async Task<IHttpActionResult> PutBook(int id, BookDto bookDto)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            var book = db.Books.Find(id);
            book.Name = bookDto.name;
            //db.Entry(book).State = EntityState.Modified;

            try
            {
                await db.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!BookExists(id))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }

            return StatusCode(HttpStatusCode.NoContent);
        }
Example #2
0
 // POST api/Book
 public async Task<IHttpActionResult> PostBook(BookDto bookDto) {
     if (ModelState.IsValid) {
         LanguageType bookLanguage;
         if (!Enum.TryParse<LanguageType>(bookDto.language, out bookLanguage)) {
             return BadRequest("Invalid book language");
         }
         var userId = User.Identity.GetUserId();
         UserBook userBook = await db.FindUserBookAsync(userId, bookDto.name, bookLanguage);
         if (userBook != null) {
             ModelState.AddModelError("name", string.Format("You already have book \"{0}\"", bookDto.name));
         } else {
             userBook = await db.CreateUserBookAsync(User.Identity.GetUserId(), bookLanguage, bookDto.name);
             var userBookDto = new UserBookDto(userBook);
             return CreatedAtRoute("DefaultApi", new { id = userBook.Id },
                 new {
                     emberDataFormat = true,
                     books = new List<dynamic> { userBookDto.BookDto },
                     userBooks = new List<dynamic> { userBookDto }
                 });
         }
     }
     return new EmberDSErrorsResult(this);
 }
 public UserBookDto(UserBook userBook, Book book)
 {
     this.id = userBook.Id;
     this.user = userBook.UserId;
     this.book = userBook.BookId;
     this.learnLevels = userBook.LearnLevels;
     this.learnDates = userBook.LearnDates;
     this.examDates = userBook.ExamDates;
     this.promoteDates = userBook.PromoteDates;
     this.BookDto = new BookDto(book, id);
     this.translations = new Dictionary<string, List<int>>();
 }