Skip to content

Commit a406a6c

Browse files
author
RahulMule
committed
Add project files.
1 parent 22b570f commit a406a6c

12 files changed

Lines changed: 254 additions & 0 deletions

DotNetWebAPI_InMemoryDatabase.sln

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.8.34330.188
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DotNetWebAPI_InMemoryDatabase", "DotNetWebAPI_InMemoryDatabase\DotNetWebAPI_InMemoryDatabase.csproj", "{F45D075B-12C3-4409-9371-54F31E617876}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{F45D075B-12C3-4409-9371-54F31E617876}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{F45D075B-12C3-4409-9371-54F31E617876}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{F45D075B-12C3-4409-9371-54F31E617876}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{F45D075B-12C3-4409-9371-54F31E617876}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {B85CDA08-E18E-4071-8556-C78066F64C33}
24+
EndGlobalSection
25+
EndGlobal
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using DotNetWebAPI_InMemoryDatabase.Models;
2+
using DotNetWebAPI_InMemoryDatabase.Services;
3+
using Microsoft.AspNetCore.Http;
4+
using Microsoft.AspNetCore.Mvc;
5+
6+
namespace DotNetWebAPI_InMemoryDatabase.Controllers
7+
{
8+
[Route("api/[controller]")]
9+
[ApiController]
10+
public class BooksController : ControllerBase
11+
{
12+
private readonly IBooks _books;
13+
public BooksController(IBooks books)
14+
{
15+
_books = books;
16+
}
17+
[HttpGet]
18+
public IActionResult GetAllBooks()
19+
{
20+
IEnumerable<Book> books = _books.GetAllBooks();
21+
return Ok(books);
22+
}
23+
[HttpPost]
24+
public IActionResult AddBook(Book book)
25+
{
26+
_books.AddBook(book);
27+
return Ok();
28+
}
29+
30+
}
31+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using DotNetWebAPI_InMemoryDatabase.Models;
2+
using Microsoft.EntityFrameworkCore;
3+
4+
namespace DotNetWebAPI_InMemoryDatabase.Data
5+
{
6+
public class BookContext : DbContext
7+
{
8+
public BookContext(DbContextOptions options) : base(options)
9+
{
10+
}
11+
public DbSet<Book> Books { get; set; }
12+
13+
}
14+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net8.0</TargetFramework>
5+
<Nullable>enable</Nullable>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<InvariantGlobalization>true</InvariantGlobalization>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.1" />
12+
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="8.0.1" />
13+
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
14+
</ItemGroup>
15+
16+
</Project>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
@DotNetWebAPI_InMemoryDatabase_HostAddress = http://localhost:5084
2+
3+
GET {{DotNetWebAPI_InMemoryDatabase_HostAddress}}/weatherforecast/
4+
Accept: application/json
5+
6+
###
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using System.ComponentModel.DataAnnotations;
2+
3+
namespace DotNetWebAPI_InMemoryDatabase.Models
4+
{
5+
public class Book
6+
{
7+
[Key]
8+
public int Id { get; set; }
9+
[Required]
10+
public string Title { get; set; }
11+
[Required]
12+
public string Description { get; set; }
13+
[Required]
14+
public string Author { get; set; }
15+
[Required]
16+
public decimal Price { get; set; }
17+
}
18+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using DotNetWebAPI_InMemoryDatabase.Data;
2+
using DotNetWebAPI_InMemoryDatabase.Services;
3+
using Microsoft.EntityFrameworkCore;
4+
5+
var builder = WebApplication.CreateBuilder(args);
6+
7+
// Add services to the container.
8+
9+
builder.Services.AddControllers();
10+
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
11+
builder.Services.AddEndpointsApiExplorer();
12+
builder.Services.AddSwaggerGen();
13+
builder.Services.AddScoped<IBooks,BookServices>();
14+
builder.Services.AddDbContext<BookContext>(options =>
15+
options.UseInMemoryDatabase("Book"));
16+
17+
18+
var app = builder.Build();
19+
20+
// Configure the HTTP request pipeline.
21+
if (app.Environment.IsDevelopment())
22+
{
23+
app.UseSwagger();
24+
app.UseSwaggerUI();
25+
}
26+
27+
app.UseHttpsRedirection();
28+
29+
app.UseAuthorization();
30+
31+
app.MapControllers();
32+
33+
app.Run();
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
{
2+
"$schema": "http://json.schemastore.org/launchsettings.json",
3+
"iisSettings": {
4+
"windowsAuthentication": false,
5+
"anonymousAuthentication": true,
6+
"iisExpress": {
7+
"applicationUrl": "http://localhost:60594",
8+
"sslPort": 44326
9+
}
10+
},
11+
"profiles": {
12+
"http": {
13+
"commandName": "Project",
14+
"dotnetRunMessages": true,
15+
"launchBrowser": true,
16+
"launchUrl": "swagger",
17+
"applicationUrl": "http://localhost:5084",
18+
"environmentVariables": {
19+
"ASPNETCORE_ENVIRONMENT": "Development"
20+
}
21+
},
22+
"https": {
23+
"commandName": "Project",
24+
"dotnetRunMessages": true,
25+
"launchBrowser": true,
26+
"launchUrl": "swagger",
27+
"applicationUrl": "https://localhost:7262;http://localhost:5084",
28+
"environmentVariables": {
29+
"ASPNETCORE_ENVIRONMENT": "Development"
30+
}
31+
},
32+
"IIS Express": {
33+
"commandName": "IISExpress",
34+
"launchBrowser": true,
35+
"launchUrl": "swagger",
36+
"environmentVariables": {
37+
"ASPNETCORE_ENVIRONMENT": "Development"
38+
}
39+
}
40+
}
41+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
using DotNetWebAPI_InMemoryDatabase.Data;
2+
using DotNetWebAPI_InMemoryDatabase.Models;
3+
using Microsoft.AspNetCore.Http.HttpResults;
4+
using Microsoft.AspNetCore.Mvc;
5+
6+
namespace DotNetWebAPI_InMemoryDatabase.Services
7+
{
8+
public class BookServices : IBooks
9+
{
10+
private readonly BookContext _context;
11+
12+
13+
public BookServices(BookContext context)
14+
{
15+
_context = context;
16+
}
17+
18+
public void AddBook(Book book)
19+
{
20+
21+
_context.Books.Add(book);
22+
_context.SaveChanges();
23+
24+
}
25+
26+
27+
public IEnumerable<Book> GetAllBooks()
28+
{
29+
30+
31+
IEnumerable<Book> books = _context.Books;
32+
if (books == null)
33+
{
34+
return null;
35+
}
36+
return books;
37+
38+
39+
}
40+
41+
}
42+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using DotNetWebAPI_InMemoryDatabase.Models;
2+
using Microsoft.AspNetCore.Mvc;
3+
4+
namespace DotNetWebAPI_InMemoryDatabase.Services
5+
{
6+
public interface IBooks
7+
{
8+
public IEnumerable<Book> GetAllBooks();
9+
public void AddBook(Book book);
10+
}
11+
}

0 commit comments

Comments
 (0)