Exemplo n.º 1
0
        public string InsertReadList(string authorName, string bookTitle, DateTime readingDate, int page, int rating)
        {
            if (string.IsNullOrEmpty(authorName))
            {
                return("Insert failed, because authorName should not be null or empty string.");
            }
            if (string.IsNullOrEmpty(bookTitle))
            {
                return("Insert failed, because bookTitle should not be null or empty string.");
            }
            if (page <= 0)
            {
                return("Insert failed, because book must have more pages than zero or less.");
            }
            if (rating <= 0 || rating > 5)
            {
                return("Insert failed, because rating must be more than one and less than five.");
            }

            using (ReadListContext db = new ReadListContext())
            {
                ReadList readList = new ReadList()
                {
                    AuthorName  = authorName,
                    BookTitle   = bookTitle,
                    ReadingDate = readingDate,
                    Page        = page,
                    Rating      = rating
                };
                db.ReadLists.Add(readList);
                db.SaveChanges();
                return("Insert successful!");
            }
        }
Exemplo n.º 2
0
 public string UpdateReadList(ReadList newReadList)
 {
     using (ReadListContext db = new ReadListContext())
     {
         ReadList temp = db.ReadLists.Find(newReadList.Id);
         if (temp == null)
         {
             return("Update failed, because row with this id does not exist.");
         }
         if (string.IsNullOrEmpty(newReadList.AuthorName))
         {
             return("Insert failed, because authorName should not be null or empty string.");
         }
         if (string.IsNullOrEmpty(newReadList.BookTitle))
         {
             return("Insert failed, because bookTitle should not be null or empty string.");
         }
         if (newReadList.Page <= 0)
         {
             return("Update failed, because book must have more pages than zero or less.");
         }
         if (newReadList.Rating <= 0 || newReadList.Rating > 5)
         {
             return("Update failed, because rating must be more than one and less than five.");
         }
         temp.AuthorName      = newReadList.AuthorName;
         temp.BookTitle       = newReadList.BookTitle;
         temp.Page            = newReadList.Page;
         temp.Rating          = newReadList.Rating;
         temp.ReadingDate     = newReadList.ReadingDate;
         db.Entry(temp).State = EntityState.Modified;
         db.SaveChanges();
         return("Update successful!");
     }
 }
Exemplo n.º 3
0
 public string DeleteById(int id)
 {
     using (ReadListContext db = new ReadListContext())
     {
         ReadList readList = db.ReadLists.Find(id);
         if (readList != null)
         {
             db.Entry(readList).State = EntityState.Deleted;
             db.SaveChanges();
             return("Delete successful!");
         }
         else
         {
             return("Delete failed, Row with this id does not exist.");
         }
     }
 }