Guest User

Untitled

a guest
May 20th, 2018
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.27 KB | None | 0 0
  1. namespace MyLibraryOfBooks.Models{
  2. public class Book{
  3. public int Id { get; set; }
  4. public string Name { get; set; }
  5.  
  6. public int? GenreId { get; set; }
  7. public virtual Genre Genre { get; set; }
  8. }}
  9.  
  10. namespace MyLibraryOfBooks.Models{
  11. public class Genre{
  12. public int Id { get; set; }
  13. public string Name { get; set; }
  14.  
  15. public virtual ICollection<Book> Books { get; set; }
  16. }}
  17.  
  18. namespace MyLibraryOfBooks.Models{
  19. public class AppDatabaseContext : DbContext{
  20. public DbSet<Book> Books { get; set; }
  21. public DbSet<Genre> Genres { get; set; }
  22. public AppDatabaseContext(){}
  23. public AppDatabaseContext(DbContextOptions<AppDatabaseContext> options) : base(options){
  24. Database.EnsureCreated())
  25. }
  26. protected override void OnModelCreating(ModelBuilder modelBuilder){
  27. modelBuilder.Entity<Book>()
  28. .HasOne(book => book.Genre)
  29. .WithMany(genre => genre.Books)
  30. .HasForeignKey(book => book.GenreId);
  31. }}}
  32.  
  33. namespace MyLibraryOfBooks.Controllers{
  34. [Route("[controller]")]
  35. public class BooksController : Controller{
  36. private readonly AppDatabaseContext _context;
  37. public BooksController(AppDatabaseContext context) { _context = context; }
  38. [HttpGet]
  39. public IEnumerable<Book> Get(){
  40. return _context.Books.Include(book => book.Genre);
  41. }
  42. [HttpGet("{id}"]
  43. public IIncludableQueryable<Book, Genre> Get(int id){
  44. return _context.Books.Where(book=>book.Id == id).Include(book =>book.Genre);
  45. }
  46. [HttpPost]
  47. public void Post([FromBody] Book book){
  48. _context.Books.Add(book);
  49. _context.SaveChanges();
  50. }
  51. [HttpPut("{id}")]
  52. public void Put(int id, [FromBody] Book book){
  53. if (id != book.Id) return;
  54. _context.Entry(book).State = EntityState.Modified;
  55. _context.SaveChanges();
  56. }
  57. [HttpDelete("{id}")]
  58. public void Delete(int id){
  59. _context.Books.Remove(_context.Books.Find(id));
  60. _context.SaveChanges();
  61. }
  62. }}
  63.  
  64. namespace MyLibraryOfBooks.Controllers{
  65. [Route("[controller]")]
  66. public class GenresController : Controller{
  67. private readonly AppDatabaseContext _context;
  68. public GenresController(AppDatabaseContext context){
  69. _context = context;
  70. }
  71. [HttpGet]
  72. public IEnumerable<Genre> Get(){
  73. return _context.Genres.Include(genre => genre.Books);
  74. }
  75. [HttpGet("{id}")]
  76. public IQueryable<Genre> Get(int id){
  77. var genre1=_context.Genres.Where(genre=>genre.Id == id).Include(genre=>genre.Books);
  78. return genre1;
  79. }
  80. [HttpPost]
  81. public void Post([FromBody] Genre genre){
  82. _context.Genres.Add(genre);
  83. _context.SaveChanges();
  84. }
  85. [HttpPut("{id}")]
  86. public void Put(int id, [FromBody] Genre genre){
  87. if (id == genre.Id) return;
  88. _context.Entry(genre).State = EntityState.Modified;
  89. _context.SaveChanges();
  90. }
  91. [HttpDelete("{id}")]
  92. public void Delete(int id){
  93. _context.Genres.Remove(_context.Genres.Find(id));
  94. _context.SaveChanges();
  95. }
  96. }}
  97.  
  98. namespace MyLibraryOfBooks{
  99. public class Startup{
  100. public Startup(IConfiguration configuration){
  101. Configuration = configuration;
  102. }
  103. private IConfiguration Configuration { get; }
  104. public void ConfigureServices(IServiceCollection services){
  105. services.AddCors(options =>{
  106. options.AddPolicy("CorsPolicy",
  107. builder => builder.AllowAnyOrigin()
  108. .AllowAnyMethod()
  109. .AllowAnyHeader()
  110. .AllowCredentials());
  111. options.AddPolicy("AllowSpecificOrigin", policy => policy.WithOrigins("http://localhost:5000"));
  112. options.AddPolicy("AllowGetMethod", policy => policy.WithMethods("GET"));
  113. });
  114. services.Configure < MvcOptions > (options => {
  115. options.Filters.Add(new CorsAuthorizationFilterFactory("CorsPolicy"));
  116. });
  117. services.AddDbContext<AppDatabaseContext>(options =>
  118. options.UseMySQL("server=localhost;port=3306;UserId=root;Password=;database=usersdb7;SslMode=none"));
  119. services.AddMvc();
  120. }
  121. public void Configure(IApplicationBuilder app, IHostingEnvironment env){
  122. app.UseCors("CorsPolicy");
  123. if (env.IsDevelopment()){
  124. app.UseDeveloperExceptionPage();
  125. }
  126. app.UseMvc();
  127. }
  128. }}
Add Comment
Please, Sign In to add comment