This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class BlogPost | |
{ | |
[Key] | |
public int PostId { get; set; } | |
[Required] | |
public string Creator { get; set; } | |
[Required] | |
public string Title { get; set; } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class BlogPostsContext : DbContext | |
{ | |
public BlogPostsContext (DbContextOptions<BlogPostsContext> options) | |
: base(options) | |
{ | |
} | |
public DbSet<BlogPost> BlogPosts { get; set; } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[Route("api/[controller]")] | |
[Produces("application/json")] | |
[ApiController] | |
public class BlogPostsController : ControllerBase | |
{ | |
private readonly BlogPostsContext _context; | |
public BlogPostsController(BlogPostsContext context) | |
{ | |
_context = context; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class DataRepository<T> : IDataRepository<T> where T : class | |
{ | |
private readonly BlogPostsContext _context; | |
public DataRepository(BlogPostsContext context) | |
{ | |
_context = context; | |
} | |
public void Add(T entity) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface IDataRepository<T> where T : class | |
{ | |
void Add(T entity); | |
void Update(T entity); | |
void Delete(T entity); | |
Task<T> SaveAsync(T entity); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[Produces("application/json")] | |
[Route("api/[controller]")] | |
[ApiController] | |
public class BlogPostsController : ControllerBase | |
{ | |
private readonly BlogPostsContext _context; | |
private readonly IDataRepository<BlogPost> _repo; | |
public BlogPostsController(BlogPostsContext context, IDataRepository<BlogPost> repo) | |
{ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
_repo.Add(blogPost); | |
var save = await _repo.SaveAsync(blogPost); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"ConnectionStrings": { | |
"BlogPostsContext": "Server=(localdb)\\mssqllocaldb;Database=BlogPostContext-d08fc301-dc66-44e2-8e02-b408c55da2cf;Trusted_Connection=True;MultipleActiveResultSets=true" | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void ConfigureServices(IServiceCollection services) | |
{ | |
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2); | |
services.AddDbContext<BlogPostsContext>(options => | |
options.UseSqlServer(Configuration.GetConnectionString("BlogPostsContext"))); | |
services.AddCors(options => | |
{ | |
options.AddPolicy("CorsPolicy", |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void Configure(IApplicationBuilder app, IHostingEnvironment env) | |
{ | |
if (env.IsDevelopment()) | |
{ | |
app.UseDeveloperExceptionPage(); | |
} | |
else | |
{ | |
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts. | |
app.UseHsts(); |
OlderNewer