Example #1
0
        public BookWriter Delete(BookWriter BookWriter)
        {
            var result = _context.Remove(BookWriter);

            _context.SaveChanges();
            return(result.Entity);
        }
Example #2
0
        public BookWriter Create(BookWriter BookWriter)
        {
            var result = _context.Add <BookWriter>(BookWriter);

            _context.SaveChanges();
            return(result.Entity);
        }
        public BookWriter Post(BookWriterDTO value)
        {
            BookWriter model = new BookWriter()
            {
                BookId   = value.BookId,
                WriterId = value.WriterId
            };

            return(IBookWriterRepository.Create(model));
        }
        public BookWriter Put(int id, BookWriterDTO value)
        {
            BookWriter model = IBookWriterRepository.Get(id);

            if (value.BookId != 0)
            {
                model.BookId = value.BookId;
            }
            if (value.WriterId != 0)
            {
                model.WriterId = value.WriterId;
            }

            return(IBookWriterRepository.Update(model));
        }
Example #5
0
        public void Post(BookDTO value)
        {
            Book model = new Book()
            {
                Name            = value.Name,
                Popularity      = value.Popularity,
                PublicationYear = value.PublicationYear
            };

            IBookRepository.Create(model);
            for (int i = 0; i < value.WriterId.Count; i++)
            {
                BookWriter BookWriter = new BookWriter()
                {
                    BookId   = model.Id,
                    WriterId = value.WriterId[i]
                };
                IBookWriterRepository.Create(BookWriter);
            }

            for (int i = 0; i < value.LibraryId.Count; i++)
            {
                BookLibrary BookLibrary = new BookLibrary()
                {
                    BookId    = model.Id,
                    LibraryId = value.LibraryId[i]
                };
                IBookLibraryRepository.Create(BookLibrary);
            }

            for (int i = 0; i < value.PublisherId.Count; i++)
            {
                BookPublisher BookPublisher = new BookPublisher()
                {
                    BookId      = model.Id,
                    PublisherId = value.PublisherId[i]
                };
                IBookPublisherRepository.Create(BookPublisher);
            }
        }
Example #6
0
        public void Put(int id, BookDTO value)
        {
            Book model = IBookRepository.Get(id);

            if (value.Name != null)
            {
                model.Name = value.Name;
            }
            if (value.Popularity != null)
            {
                model.Popularity = value.Popularity;
            }
            if (value.PublicationYear != 0)
            {
                model.PublicationYear = value.PublicationYear;
            }

            IBookRepository.Update(model);

            if (value.WriterId != null)
            {
                IEnumerable <BookWriter> MyBookWriters = IBookWriterRepository.GetAll().Where(x => x.BookId == id);
                foreach (BookWriter MyBookWriter in MyBookWriters)
                {
                    IBookWriterRepository.Delete(MyBookWriter);
                }
                for (int i = 0; i < value.WriterId.Count; i++)
                {
                    BookWriter BookWriter = new BookWriter()
                    {
                        BookId   = model.Id,
                        WriterId = value.WriterId[i]
                    };
                    IBookWriterRepository.Create(BookWriter);
                }
            }
            if (value.LibraryId != null)
            {
                IEnumerable <BookLibrary> MyLibraryBooks = IBookLibraryRepository.GetAll().Where(x => x.BookId == id);
                foreach (BookLibrary MyLibraryBook in MyLibraryBooks)
                {
                    IBookLibraryRepository.Delete(MyLibraryBook);
                }
                for (int i = 0; i < value.LibraryId.Count; i++)
                {
                    BookLibrary BookLibrary = new BookLibrary()
                    {
                        BookId    = model.Id,
                        LibraryId = value.LibraryId[i]
                    };
                    IBookLibraryRepository.Create(BookLibrary);
                }
            }
            if (value.PublisherId != null)
            {
                IEnumerable <BookPublisher> MyPublisherBooks = IBookPublisherRepository.GetAll().Where(x => x.BookId == id);
                foreach (BookPublisher MyPublisherBook in MyPublisherBooks)
                {
                    IBookPublisherRepository.Delete(MyPublisherBook);
                }
                for (int i = 0; i < value.PublisherId.Count; i++)
                {
                    BookPublisher BookPublisher = new BookPublisher()
                    {
                        BookId      = model.Id,
                        PublisherId = value.PublisherId[i]
                    };
                    IBookPublisherRepository.Create(BookPublisher);
                }
            }
        }
        public BookWriter Delete(int id)
        {
            BookWriter BookWriter = IBookWriterRepository.Get(id);

            return(IBookWriterRepository.Delete(BookWriter));
        }
Example #8
0
        public ActionResult Edit(BookEditViewModel bevm)
        {
            if (bevm.BookViewModel.Contents == null && !string.IsNullOrEmpty(bevm.OldContent))
            {
                ModelState["BookViewModel.Contents"].Errors.Clear();
            }
            if (ModelState.IsValid)
            {
                var book = db.Books
                           .Include(b => b.Writers)
                           .Include(b => b.Labels)
                           .Include(b => b.Category)
                           .Single(b => b.BookID == bevm.BookViewModel.BookID);

                var    directory = Path.Combine(Server.MapPath("~/App_Data/uploads"), book.BookID.ToString());
                string filename;
                if (bevm.BookViewModel.Contents != null)
                {
                    var oldfilepath = Path.Combine(directory, book.Contents);
                    if (System.IO.File.Exists(oldfilepath))
                    {
                        System.IO.File.Delete(oldfilepath);
                    }
                    var extension = Path.GetExtension(bevm.BookViewModel.Contents.FileName);
                    filename = bevm.BookViewModel.BookID + "_contents" + extension;
                    bevm.BookViewModel.Contents.SaveAs(Path.Combine(directory, filename));
                }
                else
                {
                    filename = bevm.OldContent;
                }

                book.Amount      = bevm.BookViewModel.Amount;
                book.Contents    = filename;
                book.Title       = bevm.BookViewModel.Title;
                book.ISBN        = bevm.BookViewModel.ISBN;
                book.Year        = bevm.BookViewModel.Year;
                book.Description = bevm.BookViewModel.Description;
                book.CategoryID  = bevm.BookViewModel.CategoryID;
                book.Category    = db.Categories.FirstOrDefault(x => x.CategoryID == bevm.BookViewModel.CategoryID);
                var selectedwriterslist = new List <Writer>();
                var selectedlabelslist  = new List <Label>();

                for (int i = 0; i < bevm.BookViewModel.SelectedWriters.Length; i++)
                {
                    var id     = bevm.BookViewModel.SelectedWriters[i];
                    var writer = db.Writers.FirstOrDefault(x => x.WriterID == id);
                    if (writer != null)
                    {
                        selectedwriterslist.Add(writer);
                    }
                }
                db.SaveChanges();
                for (int i = 0; i < bevm.BookViewModel.SelectedLabels.Length; i++)
                {
                    var id    = bevm.BookViewModel.SelectedLabels[i];
                    var label = db.Labels.FirstOrDefault(x => x.LabelID == id);
                    if (label != null)
                    {
                        selectedlabelslist.Add(label);
                    }
                }

                //Check Writers
                var actualwriters = db.Writers.Where(w => w.BookWriters.Any(b => b.BookID == bevm.BookViewModel.BookID)).ToList();
                db.SaveChanges();
                foreach (var dbWriter in db.Writers.ToList())
                {
                    if (selectedwriterslist.Contains(dbWriter))
                    {
                        if (!actualwriters.Contains(dbWriter))
                        {
                            var bw = new BookWriter()
                            {
                                WriterID = dbWriter.WriterID,
                                BookID   = bevm.BookViewModel.BookID
                            };
                            db.BookWriters.Add(bw);
                        }
                    }
                    else
                    {
                        if (actualwriters.Contains(dbWriter))
                        {
                            var bw =
                                db.BookWriters.FirstOrDefault(
                                    x => x.WriterID == dbWriter.WriterID && x.BookID == bevm.BookViewModel.BookID);
                            db.BookWriters.Remove(bw);
                        }
                    }
                    db.SaveChanges();
                }

                //Check Labels
                var actuallabels = db.Labels.Where(w => w.BookLabels.Any(b => b.BookID == bevm.BookViewModel.BookID)).ToList();
                db.SaveChanges();
                foreach (var dbLabel in db.Labels.ToList())
                {
                    if (selectedlabelslist.Contains(dbLabel))
                    {
                        if (!actuallabels.Contains(dbLabel))
                        {
                            var bl = new BookLabel()
                            {
                                BookID  = bevm.BookViewModel.BookID,
                                LabelID = dbLabel.LabelID
                            };
                            db.BookLabels.Add(bl);
                        }
                    }
                    else
                    {
                        if (actuallabels.Contains(dbLabel))
                        {
                            var bl =
                                db.BookLabels.FirstOrDefault(x => x.LabelID == dbLabel.LabelID && x.BookID == bevm.BookViewModel.BookID);
                            db.BookLabels.Remove(bl);
                        }
                    }
                    db.SaveChanges();
                }

                //Check Files
                List <string> editedfilessource;
                List <string> editedfilestext;
                if (bevm.OldFiles != null)
                {
                    editedfilessource = bevm.OldFiles;
                }
                else
                {
                    editedfilessource = new List <string>();
                }
                if (bevm.OldFilesText != null)
                {
                    editedfilestext = bevm.OldFilesText;
                }
                else
                {
                    editedfilestext = new List <string>();
                }
                var allfiles       = db.Files.Where(f => f.BookID == bevm.BookViewModel.BookID).ToList();
                var allfilessource = allfiles.Select(f => f.Source).ToList();

                //Add new files to list of sources and disc
                if (bevm.BookViewModel.Files != null)
                {
                    for (var i = 0; i < bevm.BookViewModel.Files.Count; i++)
                    {
                        if (bevm.BookViewModel.Files[i] != null)
                        {
                            var source = bevm.BookViewModel.Files[i].FileName;
                            var path   = Path.Combine(directory, source);
                            bevm.BookViewModel.Files [i].SaveAs(path);
                            editedfilessource.Add(source);
                            editedfilestext.Add(bevm.BookViewModel.FilesNames [i]);
                        }
                    }
                }

                foreach (var dbfile in allfiles)
                {
                    //if in base exist file that we remove while editing, then remove it from db and from disc
                    if (!editedfilessource.Contains(dbfile.Source))
                    {
                        if (System.IO.File.Exists(Path.Combine(directory, dbfile.Source)))
                        {
                            System.IO.File.Delete(Path.Combine(directory, dbfile.Source));
                        }

                        db.Files.Remove(dbfile);
                    }
                }

                if (editedfilessource.Count > 0)
                {
                    for (var i = 0; i < editedfilessource.Count; i++)
                    {
                        //if file that we edited is not in db, which means it's new file, add it to db
                        if (!allfilessource.Contains(editedfilessource [i]))
                        {
                            File file = new File()
                            {
                                BookID = bevm.BookViewModel.BookID,
                                Name   = editedfilestext[i],
                                Source = editedfilessource[i]
                            };
                            db.Files.Add(file);
                            db.SaveChanges();
                        }
                        else
                        {
                            //find file text in db
                            var source   = editedfilessource[i];
                            var filetext =
                                db.Files.FirstOrDefault(
                                    f => f.BookID == bevm.BookViewModel.BookID && f.Source == source);

                            //if it is in db, check if text is still the same
                            //if it's not, change it
                            if (editedfilestext != null)
                            {
                                if (editedfilestext [i] != filetext.Name)
                                {
                                    filetext.Name = editedfilestext [i];
                                    db.SaveChanges();
                                }
                            }
                        }
                        //if its already in db and text still the same, nothing to do
                        db.SaveChanges();
                    }
                }


                db.Entry(book).State = EntityState.Modified;
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            //Not valid
            SetViewBag();
            return(View(bevm));
        }
Example #9
0
        public ActionResult Create(BookViewModel bvm)
        {
            if (ModelState.IsValid)
            {
                var extension = Path.GetExtension(bvm.Contents.FileName);

                Book book = new Book()
                {
                    AddDate     = DateTime.Now,
                    Amount      = bvm.Amount,
                    CategoryID  = bvm.CategoryID,
                    Year        = bvm.Year,
                    Title       = bvm.Title,
                    ISBN        = bvm.ISBN,
                    Description = bvm.Description,
                    Writers     = new List <BookWriter>(),
                    Labels      = new List <BookLabel>()
                };
                db.Books.Add(book);
                db.SaveChanges();
                var filename = book.BookID + "_contents" + extension;
                book.Contents = filename;
                var directory = Path.Combine(Server.MapPath("~/App_Data/uploads"), book.BookID.ToString());
                Directory.CreateDirectory(directory);
                var path = Path.Combine(directory, filename);
                bvm.Contents.SaveAs(path);
                db.SaveChanges();
                for (int i = 0; i < bvm.SelectedWriters.Length; i++)
                {
                    var id     = bvm.SelectedWriters[i];
                    var writer = db.Writers.FirstOrDefault(x => x.WriterID == id);
                    if (writer != null)
                    {
                        var bw = new BookWriter()
                        {
                            BookID   = book.BookID,
                            WriterID = writer.WriterID
                        };
                        db.BookWriters.Add(bw);
                        db.SaveChanges();
                    }
                }
                for (int i = 0; i < bvm.SelectedLabels.Length; i++)
                {
                    var id    = bvm.SelectedLabels[i];
                    var label = db.Labels.FirstOrDefault(x => x.LabelID == id);
                    if (label != null)
                    {
                        var bl = new BookLabel()
                        {
                            BookID  = book.BookID,
                            LabelID = label.LabelID
                        };
                        db.BookLabels.Add(bl);
                        db.SaveChanges();
                    }
                }
                db.Entry(book).State = EntityState.Modified;
                db.SaveChanges();

                for (var i = 0; i < bvm.Files.Count; i++)
                {
                    if (bvm.Files[i] != null)
                    {
                        File file = new File();
                        file.BookID = book.BookID;
                        file.Book   = book;
                        file.Name   = bvm.FilesNames[i];
                        file.Source = bvm.Files[i].FileName;
                        bvm.Files[i].SaveAs(Path.Combine(directory, bvm.Files [i].FileName));
                        db.Files.Add(file);
                        db.SaveChanges();
                    }
                }


                return(RedirectToAction("Index"));
            }


            IEnumerable <SelectListItem> writers = from w in db.Writers
                                                   select new SelectListItem
            {
                Value = w.WriterID.ToString(),
                Text  = w.Name + " " + w.Surname
            };

            ViewBag.CategoryID = new SelectList(db.Categories, "CategoryID", "Name");
            ViewBag.Writers    = new SelectList(writers, "Value", "Text");
            ViewBag.Labels     = new SelectList(db.Labels, "LabelID", "Name");
            return(View(bvm));
        }
Example #10
0
 public BookWriter Update(BookWriter BookWriter)
 {
     _context.Entry(BookWriter).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
     _context.SaveChanges();
     return(BookWriter);
 }