Example #1
0
        public IHttpActionResult PutTitle(string id, Title title)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != title.ISBN)
            {
                return(BadRequest());
            }

            db.Entry(title).State = EntityState.Modified;

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!TitleExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(StatusCode(HttpStatusCode.NoContent));
        }
        public IHttpActionResult Postcat(CategoryVM cat)
        {
            bool catFlag = isAddedBefor(cat.Name);

            if (catFlag)
            {
                ModelState.AddModelError("cat", "Categoryl already exists");
            }
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            Category newcat = new API_Server.Category();

            newcat = ConvertClass.VMToCategory(cat);
            db.Categories.Add(newcat);
            db.SaveChanges();


            var catId =
                (from c in db.Categories
                 where c.Name == cat.Name
                 select c.Id).FirstOrDefault();

            return(Ok(catId));
        }
Example #3
0
        public IHttpActionResult PutBook(Guid id, Book book)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != book.GUID)
            {
                return(BadRequest());
            }

            book.UpdatedAt       = DateTime.Now;
            db.Entry(book).State = EntityState.Modified;

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!BookExists(id))
                {
                    return(StatusCode(HttpStatusCode.NotFound));
                }
                else
                {
                    throw;
                }
            }

            return(Ok(0));
        }
Example #4
0
        public IHttpActionResult PutAuthor(int id, Author author)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != author.AuthorID)
            {
                return(BadRequest());
            }

            db.Entry(author).State = EntityState.Modified;

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!AuthorExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(StatusCode(HttpStatusCode.NoContent));
        }
Example #5
0
        public ActionResult Create([Bind(Include = "AuthorID,FirstName,LastName")] Author author)
        {
            if (ModelState.IsValid)
            {
                db.Authors.Add(author);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(author));
        }
Example #6
0
        public ActionResult Create([Bind(Include = "BookID,Title,Author,Publisher,PageNumber")] Book book)
        {
            if (ModelState.IsValid)
            {
                db.Book.Add(book);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(book));
        }
Example #7
0
        public ActionResult Create([Bind(Include = "ISBN,Title1,EditionNumber,Copyright")] Title title)
        {
            if (ModelState.IsValid)
            {
                db.Titles.Add(title);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(title));
        }
Example #8
0
 //метод додавання нової книги.
 //[FromBody]  - інформація про книгу передається в тілі запиту
 public IHttpActionResult Post([FromBody] Book book)
 {
     // спробувати додати кнгу
     try
     {
         db.Book.Add(book);
         db.SaveChanges();
         return(Ok(book));
     }
     // якщо трапиться помилка - повернути Internal Server Error
     catch (Exception error)
     {
         return(InternalServerError(error));
     }
 }
Example #9
0
        //[Route("api/Books/PostPerson")]

        public string PostPerson(Person person)
        {
            db.Authors.Add(new Author {
                FirstName = person.fname, LastName = person.lname
            });
            db.SaveChanges();
            return("ok");
        }
Example #10
0
        public IHttpActionResult PostRequest(Request request)
        {
            if (request == null)
            {
                // save the item here
                return(Ok(request));
            }
            var requestCheck = db.Requests
                               .Where(b => b.Requester_Id == request.Requester_Id && b.Book_Id == request.Book_Id && b.Seller_Id == request.Seller_Id)
                               .FirstOrDefault();

            if (requestCheck != null)
            {
                // save the item here
                return(Ok(requestCheck));
            }
            request.Date = DateTime.Now;
            request.User = db.Users.Find(request.Requester_Id);
            request.Seller_Approved_book = (from AB in db.Seller_Approved_book
                                            where AB.Book_Id == request.Book_Id
                                            select AB).FirstOrDefault();
            request.Acctepted = "No";
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            db.Requests.Add(request);
            db.SaveChanges();

            return(Ok(request));
        }
Example #11
0
 public void Delete(decimal id)
 {
     using (var data = new BooksEntities())
     {
         var my = (from e in data.book select e).Where(z => z.Id.Equals(id)).FirstOrDefault();
         data.book.Remove(my);
         data.SaveChanges();
     }
 }
Example #12
0
        public void Add(Book book)
        {
            using (var context = new BooksEntities())
            {
                var newEnt = Mapper.Map <BookDb>(book);
                context.Books.Add(newEnt);

                context.SaveChanges();
            }
        }
 public IHttpActionResult PostReview(Message msg)
 {
     if (!ModelState.IsValid)
     {
         return(BadRequest(ModelState));
     }
     msg.Msg_Date = DateTime.Now;
     db.Messages.Add(msg);
     db.SaveChanges();
     return(Ok(msg));
 }
        public IHttpActionResult addUser(User u)
        {
            bool email = isAddedBefor(u.Email);

            if (email)
            {
                ModelState.AddModelError("email", "emial added before");
            }
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            db.Users.Add(u);
            db.SaveChanges();
            Point point = new Point();

            point.User_Id = u.Id;
            point.Point1  = 25;
            db.Points.Add(point);
            db.SaveChanges();
            return(Ok(u));
        }
Example #15
0
 static void Main(string[] args)
 {
     SetDir();
     using (var ctx = new BooksEntities())
     {
         List <Author> authors = ctx.Authors.ToList();
         foreach (Author author in authors)
         {
             author.SSN = RandomString(9);
         }
         ctx.SaveChanges();
     }
 }
        public IHttpActionResult PostReview(ReviewVM reviewVM)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            reviewVM.Date = DateTime.Now;
            Review review = ConvertClass.ReviewVMToModel(reviewVM);

            db.Reviews.Add(review);
            db.SaveChanges();
            return(Ok(review));
        }
Example #17
0
        private void authorBindingNavigatorSaveItem_Click(object sender, EventArgs e)
        {
            Validate();
            authorBindingSource.EndEdit();

            try
            {
                dbContext.SaveChanges();
            }catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
Example #18
0
        private static void Main(string[] args)
        {
            var r = new Random();

            using (var db = new BooksEntities())
            {
                var authors = db.Authors;

                authors.Add(new Author
                {
                    FirstName = "Damon",
                    LastName  = "German",
                    Titles    = new List <Title>
                    {
                        new Title
                        {
                            Title1        = "Hello World",
                            Copyright     = "2018",
                            EditionNumber = 1,
                            ISBN          = GetRandomString(r, 15)
                        }
                    }
                });

                db.SaveChanges();

                foreach (var author in authors)
                {
                    author.FirstName = author.FirstName.ToUpper();
                    author.LastName  = author.LastName.ToUpper();
                }

                db.SaveChanges();

                DisplayAuthorsAndISBNs(db);
                DisplayAuthorsAndTitles(db);
            }
        }
Example #19
0
 public ActionResult Create([Bind(Exclude = "Id")] Book bookToCreate)
 {
     try
     {
         // TODO: Add insert logic here
         _entities.Table.Add(bookToCreate);
         _entities.SaveChanges();
         return(RedirectToAction("Index"));
     }
     catch
     {
         return(View());
     }
 }
Example #20
0
 public void Remove(int id)
 {
     using (var context = new BooksEntities())
     {
         var bookToRemove = context.Books.SingleOrDefault(b => b.ID == id);
         if (bookToRemove != null)
         {
             context.Entry(bookToRemove).State = EntityState.Deleted;
             context.SaveChanges();
         }
         else
         {
             throw new DBConcurrencyException();
         }
     }
 }
Example #21
0
        public void update(BookModel a)
        {
            using (var data = new BooksEntities())
            {
                var my = (from d in data.book where d.Id == a.id select d).FirstOrDefault();
                my.Name        = a.Name;
                my.author      = a.author;
                my.editorial   = a.editorial;
                my.edition     = a.edition;
                my.publication = a.publication;
                my.id_city     = a.id_city;
                my.Country     = a.Country;

                data.SaveChanges();
            }
        }
Example #22
0
 public void Update(Book book)
 {
     using (var context = new BooksEntities())
     {
         var bookToUpdate = context.Books.SingleOrDefault(b => b.ID == book.Id);
         if (bookToUpdate != null)
         {
             Mapper.Map(book, bookToUpdate);
             context.SaveChanges();
         }
         else
         {
             throw new DBConcurrencyException();
         }
     }
 }
Example #23
0
        //to create a record
        public void Create(BookModel u)
        {
            using (var data = new BooksEntities())
            {
                book bok = new book();
                bok.Name        = u.Name;
                bok.author      = u.author;
                bok.editorial   = u.editorial;
                bok.edition     = u.edition;
                bok.publication = u.publication;
                bok.id_city     = u.id_city;
                bok.Country     = u.Country;

                data.book.Add(bok);
                data.SaveChanges();
            }
        }
        public IHttpActionResult UserActivate(int bookID)
        {
            var book = db.Seller_Approved_book.Where(b => b.Book_Id == bookID).FirstOrDefault();

            if (book.Approved == "Yes")
            {
                book.Approved = "No";
            }
            else
            {
                book.Approved = "Yes";
            }
            db.SaveChanges();
            if (book == null)
            {
                return(Ok("user NotFound"));
            }
            return(Ok(book));
        }
Example #25
0
 public void Save()
 {
     context.SaveChanges();
 }
Example #26
0
        private static Author UpdateAge(Author author, int age)
        {
            using (var db = new BooksEntities())
            {
                var oldAuther = db.Authors.Find(author.AuthorID);
                oldAuther.Age = age;
                db.SaveChanges();

                return oldAuther;
            }
        }
Example #27
0
        private static Author DeleteAuthorAuthor(Author author)
        {
            using (var db = new BooksEntities())
            {
                var oldAuthor = db.Authors.Find(author.AuthorID);
                db.Authors.Remove(oldAuthor);
                db.SaveChanges();

                return author;
            }
        }
        public IHttpActionResult Put(AddbooVM editbook)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest("Not a valid model"));
            }

            bool govexsist  = false;
            bool cityexsist = false;
            int  govid;
            int  citid;
            var  govnames = (from i in db.Governates
                             select i.Name).ToList();

            foreach (var item in govnames)
            {
                if (item == editbook.Governate)
                {
                    govexsist = true;
                }
            }

            if (govexsist)
            {
                govid = (from i in db.Governates
                         where i.Name == editbook.Governate
                         select i.Id).First();
            }
            else
            {
                Governate Gov = new Governate();
                Gov.Name = editbook.Governate;
                db.Governates.Add(Gov);
                db.SaveChanges();

                govid = (from i in db.Governates
                         where i.Name == editbook.Governate
                         select i.Id).First();
            }

            var citynames = (from i in db.Cities
                             select i.Name).ToList();

            foreach (var item in citynames)
            {
                if (item == editbook.City)
                {
                    cityexsist = true;
                }
            }

            if (cityexsist)
            {
                citid = (from i in db.Cities
                         where i.Name == editbook.City
                         select i.Id).First();
            }
            else
            {
                City cit = new City();
                cit.Name         = editbook.City;
                cit.Governate_Id = govid;
                db.Cities.Add(cit);
                db.SaveChanges();

                citid = (from i in db.Cities
                         where i.Name == editbook.City
                         select i.Id).First();
            }
            var subid = (from i in db.SubCategories
                         where i.Name == editbook.SubCatogry
                         select i.Id).First();


            var imglist = (from i in db.Images
                           where i.Book_Id == editbook.BookId
                           select i.Id).ToList();

            foreach (var item in imglist)
            {
                var img = db.Images.Where(e => e.Id == item).First();
                db.Images.Remove(img);
                db.SaveChanges();
            }

            using (var ctx = new BooksEntities())
            {
                var existingbook = ctx.Books.Where(s => s.Id == editbook.BookId)
                                   .FirstOrDefault <Book>();

                if (existingbook != null)
                {
                    existingbook.Name        = editbook.BookName;
                    existingbook.Points      = editbook.Points;
                    existingbook.Discription = editbook.Discription;

                    existingbook.City_Id        = citid;
                    existingbook.Subcategory_Id = subid;
                    ctx.SaveChanges();

                    foreach (var item in editbook.Img)
                    {
                        if (item != null)
                        {
                            Image im = new Image();

                            im.Book_Id = editbook.BookId;
                            im.Img_URL = item;
                            ctx.Images.Add(im);
                            ctx.SaveChanges();
                        }
                    }
                }
                else
                {
                    return(NotFound());
                }
            }

            return(Ok(editbook));
        }
        public IHttpActionResult postbook(AddbooVM addbook)
        {
            bool   govexsist  = false;
            bool   cityexsist = false;
            int    govid;
            int    citid;
            string GoverStr = addbook.Governate;
            string cityStr  = addbook.Governate;

            if (addbook != null)
            {
                if (addbook.Governate.Contains(" Governorate"))
                {
                    addbook.Governate = addbook.Governate.Replace(" Governorate", "");
                }
                if (addbook.City.Contains(" Governorate"))
                {
                    addbook.City = addbook.City.Replace(" Governorate", "");
                }
                if (addbook.City.Contains("Qesm"))
                {
                    addbook.City = addbook.City.Split(' ').Last();
                    if (addbook.City == "Asyut")
                    {
                        addbook.City = "Assuit";
                    }
                }
                var govnames = (from i in db.Governates
                                select i.Name).ToList();
                foreach (var item in govnames)
                {
                    if (GoverStr == addbook.Governate)
                    {
                        govexsist = true;
                    }
                }

                if (govexsist)
                {
                    govid = (from i in db.Governates
                             where i.Name == addbook.Governate
                             select i.Id).First();
                }
                else
                {
                    Governate Gov = new Governate();
                    Gov.Name = addbook.Governate;
                    db.Governates.Add(Gov);
                    db.SaveChanges();

                    govid = (from i in db.Governates
                             where i.Name == addbook.Governate
                             select i.Id).First();
                }

                var citynames = (from i in db.Cities
                                 select i.Name).ToList();
                foreach (var item in citynames)
                {
                    if (item == addbook.City)
                    {
                        cityexsist = true;
                    }
                }

                if (cityexsist)
                {
                    citid = (from i in db.Cities
                             where i.Name == addbook.City
                             select i.Id).First();
                }
                else
                {
                    City cit = new City();
                    cit.Name         = addbook.City;
                    cit.Governate_Id = govid;
                    db.Cities.Add(cit);
                    db.SaveChanges();

                    citid = (from i in db.Cities
                             where i.Name == addbook.City
                             select i.Id).First();
                }
                var subid = (from i in db.SubCategories
                             where i.Name == addbook.SubCatogry
                             select i.Id).First();

                Book book = new Book();
                book.Name           = addbook.BookName;
                book.Points         = addbook.Points;
                book.Discription    = addbook.Discription;
                book.Date_Uploaded  = DateTime.Now;
                book.City_Id        = citid;
                book.Subcategory_Id = subid;

                db.Books.Add(book);
                db.SaveChanges();

                var bookid = (from i in db.Books
                              where i.Name == addbook.BookName && i.Points == addbook.Points && i.Discription == addbook.Discription && i.City_Id == citid
                              select i.Id).First();
                Seller_Approved_book sab = new Seller_Approved_book();
                sab.Book_Id   = bookid;
                sab.Seller_Id = addbook.UserId;
                sab.Approved  = "no";
                db.Seller_Approved_book.Add(sab);
                db.SaveChanges();

                foreach (var item in addbook.Img)
                {
                    if (item != null)
                    {
                        Image im = new Image();
                        im.Book_Id = bookid;
                        im.Img_URL = item;
                        db.Images.Add(im);
                        db.SaveChanges();
                    }
                }
            }

            return(Ok(addbook));
        }
Example #30
0
 public void AddBook(Book book)
 {
     context.Book.Add(book);
     context.SaveChanges();
 }
Example #31
0
        private static Author UpdateAuthor(Author author)
        {
            using (var db = new BooksEntities())
            {
                var oldAuthor = db.Authors.Find(author.AuthorID);
                oldAuthor.FirstName = author.FirstName;
                oldAuthor.LastName = author.LastName;
                db.SaveChanges();

                return author;
            }
        }
Example #32
0
 public void Add(Book data)
 {
     context.Books.Add(data);
     context.SaveChanges();
 }
Example #33
0
        private static Author AddAuthor(string firstName, string lastName)
        {
            using (var db = new BooksEntities())
            {
                Author author = new Author();
                author.FirstName = firstName;
                author.LastName = lastName;
                db.Authors.Add(author);
                db.SaveChanges();

                return author;
            }
        }
        public IHttpActionResult Delete(int id)
        {
            bool selll = false;

            if (id <= 0)
            {
                return(BadRequest("Not a valid Book id"));
            }
            var selby = (from i in db.Seller_Buyer_Book
                         select i.Book_Id).ToList();

            foreach (var item in selby)
            {
                if (item == id)
                {
                    selll = true;
                }
            }

            if (selll)
            {
                return(BadRequest("Not a valid Book id"));
            }
            else
            {
                using (var ctx = new BooksEntities())
                {
                    var bookrequest = ctx.Requests
                                      .Where(s => s.Book_Id == id)
                                      .FirstOrDefault();
                    if (bookrequest != null)
                    {
                        ctx.Entry(bookrequest).State = System.Data.Entity.EntityState.Deleted;
                        ctx.SaveChanges();
                    }
                    var bookapproved = ctx.Seller_Approved_book
                                       .Where(s => s.Book_Id == id)
                                       .FirstOrDefault();
                    if (bookapproved != null)
                    {
                        ctx.Entry(bookapproved).State = System.Data.Entity.EntityState.Deleted;
                        ctx.SaveChanges();
                    }

                    var imglist = (from i in db.Images
                                   where i.Book_Id == id
                                   select i.Id).ToList();

                    foreach (var item in imglist)
                    {
                        var img = db.Images.Where(e => e.Id == item).First();
                        if (img != null)
                        {
                            db.Images.Remove(img);
                            db.SaveChanges();
                        }
                    }
                    var book = ctx.Books
                               .Where(s => s.Id == id)
                               .FirstOrDefault();

                    ctx.Entry(book).State = System.Data.Entity.EntityState.Deleted;
                    ctx.SaveChanges();
                }
            }

            return(Ok());
        }