Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace MyLibraryOfBooks.Models{
- public class Book{
- public int Id { get; set; }
- public string Name { get; set; }
- public int? GenreId { get; set; }
- public virtual Genre Genre { get; set; }
- }}
- namespace MyLibraryOfBooks.Models{
- public class Genre{
- public int Id { get; set; }
- public string Name { get; set; }
- public virtual ICollection<Book> Books { get; set; }
- }}
- namespace MyLibraryOfBooks.Models{
- public class AppDatabaseContext : DbContext{
- public DbSet<Book> Books { get; set; }
- public DbSet<Genre> Genres { get; set; }
- public AppDatabaseContext(){}
- public AppDatabaseContext(DbContextOptions<AppDatabaseContext> options) : base(options){
- Database.EnsureCreated())
- }
- protected override void OnModelCreating(ModelBuilder modelBuilder){
- modelBuilder.Entity<Book>()
- .HasOne(book => book.Genre)
- .WithMany(genre => genre.Books)
- .HasForeignKey(book => book.GenreId);
- }}}
- namespace MyLibraryOfBooks.Controllers{
- [Route("[controller]")]
- public class BooksController : Controller{
- private readonly AppDatabaseContext _context;
- public BooksController(AppDatabaseContext context) { _context = context; }
- [HttpGet]
- public IEnumerable<Book> Get(){
- return _context.Books.Include(book => book.Genre);
- }
- [HttpGet("{id}"]
- public IIncludableQueryable<Book, Genre> Get(int id){
- return _context.Books.Where(book=>book.Id == id).Include(book =>book.Genre);
- }
- [HttpPost]
- public void Post([FromBody] Book book){
- _context.Books.Add(book);
- _context.SaveChanges();
- }
- [HttpPut("{id}")]
- public void Put(int id, [FromBody] Book book){
- if (id != book.Id) return;
- _context.Entry(book).State = EntityState.Modified;
- _context.SaveChanges();
- }
- [HttpDelete("{id}")]
- public void Delete(int id){
- _context.Books.Remove(_context.Books.Find(id));
- _context.SaveChanges();
- }
- }}
- namespace MyLibraryOfBooks.Controllers{
- [Route("[controller]")]
- public class GenresController : Controller{
- private readonly AppDatabaseContext _context;
- public GenresController(AppDatabaseContext context){
- _context = context;
- }
- [HttpGet]
- public IEnumerable<Genre> Get(){
- return _context.Genres.Include(genre => genre.Books);
- }
- [HttpGet("{id}")]
- public IQueryable<Genre> Get(int id){
- var genre1=_context.Genres.Where(genre=>genre.Id == id).Include(genre=>genre.Books);
- return genre1;
- }
- [HttpPost]
- public void Post([FromBody] Genre genre){
- _context.Genres.Add(genre);
- _context.SaveChanges();
- }
- [HttpPut("{id}")]
- public void Put(int id, [FromBody] Genre genre){
- if (id == genre.Id) return;
- _context.Entry(genre).State = EntityState.Modified;
- _context.SaveChanges();
- }
- [HttpDelete("{id}")]
- public void Delete(int id){
- _context.Genres.Remove(_context.Genres.Find(id));
- _context.SaveChanges();
- }
- }}
- namespace MyLibraryOfBooks{
- public class Startup{
- public Startup(IConfiguration configuration){
- Configuration = configuration;
- }
- private IConfiguration Configuration { get; }
- public void ConfigureServices(IServiceCollection services){
- services.AddCors(options =>{
- options.AddPolicy("CorsPolicy",
- builder => builder.AllowAnyOrigin()
- .AllowAnyMethod()
- .AllowAnyHeader()
- .AllowCredentials());
- options.AddPolicy("AllowSpecificOrigin", policy => policy.WithOrigins("http://localhost:5000"));
- options.AddPolicy("AllowGetMethod", policy => policy.WithMethods("GET"));
- });
- services.Configure < MvcOptions > (options => {
- options.Filters.Add(new CorsAuthorizationFilterFactory("CorsPolicy"));
- });
- services.AddDbContext<AppDatabaseContext>(options =>
- options.UseMySQL("server=localhost;port=3306;UserId=root;Password=;database=usersdb7;SslMode=none"));
- services.AddMvc();
- }
- public void Configure(IApplicationBuilder app, IHostingEnvironment env){
- app.UseCors("CorsPolicy");
- if (env.IsDevelopment()){
- app.UseDeveloperExceptionPage();
- }
- app.UseMvc();
- }
- }}
Add Comment
Please, Sign In to add comment