Last active
October 29, 2021 09:53
-
-
Save georgekosmidis/930b66ba69e186c833355d370ecf4b75 to your computer and use it in GitHub Desktop.
The code snippets of this gist are part of https://blog.georgekosmidis.net/2019/11/02/using-litedb-in-an-asp-net-core-api/
This file contains hidden or 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
| "LiteDbOptions": { | |
| "DatabaseLocation": "LiteDb/LiteDbTest.db" | |
| } |
This file contains hidden or 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
| dotnet new webapi | |
| dotnet add package LiteDB | |
| dotnet build |
This file contains hidden or 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 LiteDbContext : ILiteDbContext | |
| { | |
| public LiteDatabase Database { get; } | |
| public LiteDbContext(IOptions<LiteDbOptions> options) | |
| { | |
| Database = new LiteDatabase(options.Value.DatabaseLocation); | |
| } | |
| } |
This file contains hidden or 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 LiteDbWeatherForecastService : ILiteDbWeatherForecastService | |
| { | |
| private LiteDatabase _liteDb; | |
| public LiteDbWeatherForecastService(ILiteDbContext liteDbContext) | |
| { | |
| _liteDb = liteDbContext.Database; | |
| } | |
| public IEnumerable<WeatherForecast> FindAll() | |
| { | |
| return _liteDb.GetCollection<WeatherForecast>("WeatherForecast") | |
| .FindAll(); | |
| } | |
| public WeatherForecast FindOne(int id) | |
| { | |
| return _liteDb.GetCollection<WeatherForecast>("WeatherForecast") | |
| .Find(x => x.Id == id).FirstOrDefault(); | |
| } | |
| public bool Insert(WeatherForecast forecast) | |
| { | |
| return _liteDb.GetCollection<WeatherForecast>("Api") | |
| .Insert(forecast); | |
| } | |
| public bool Update(WeatherForecast forecast) | |
| { | |
| return _liteDb.GetCollection<WeatherForecast>("Api") | |
| .Update(forecast); | |
| } | |
| public int Delete(int id) | |
| { | |
| return _liteDb.GetCollection<WeatherForecast>("Api") | |
| .Delete(x => x.Id == id); | |
| } | |
| } |
This file contains hidden or 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
| services.AddSingleton<ILiteDbContext, LiteDbContext>(); |
This file contains hidden or 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
| services.AddTransient<ILiteDbWeatherForecastService, LiteDbWeatherForecastService>(); |
This file contains hidden or 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 WeatherForecast | |
| { | |
| public int Id { get; set; }//New addition, Id wasn't there | |
| public DateTime Date { get; set; } | |
| public int TemperatureC { get; set; } | |
| public int TemperatureF => 32 + (int)(TemperatureC / 0.5556); | |
| public string Summary { get; set; } | |
| } |
This file contains hidden or 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
| [ApiController] | |
| [Route("[controller]")] | |
| public class WeatherForecastController : ControllerBase | |
| { | |
| private readonly ILogger<WeatherForecastController> _logger; | |
| private readonly ILiteDbWeatherForecastService _forecastDbService; | |
| public WeatherForecastController(ILogger<WeatherForecastController> logger, ILiteDbWeatherForecastService forecastDbService) | |
| { | |
| _forecastDbService = forecastDbService; | |
| _logger = logger; | |
| } | |
| [HttpGet] | |
| public IEnumerable<WeatherForecast> Get() | |
| { | |
| return _forecastDbService.FindAll(); | |
| } | |
| [HttpGet("{id}", Name = "FindOne")] | |
| public ActionResult<WeatherForecast> Get(int id) | |
| { | |
| var result = _forecastDbService.FindOne(id); | |
| if (result != default) | |
| return Ok(_forecastDbService.FindOne(id)); | |
| else | |
| return NotFound(); | |
| } | |
| [HttpPost] | |
| public ActionResult<WeatherForecast> Insert(WeatherForecast dto) | |
| { | |
| var id = _forecastDbService.Insert(dto); | |
| if (id != default) | |
| return CreatedAtAction("FindOne", _forecastDbService.FindOne(id)); | |
| else | |
| return BadRequest(); | |
| } | |
| [HttpPut] | |
| public ActionResult<WeatherForecast> Update(WeatherForecast dto) | |
| { | |
| var result = _forecastDbService.Update(dto); | |
| if (result) | |
| return NoContent(); | |
| else | |
| return NotFound(); | |
| } | |
| [HttpDelete("{id}")] | |
| public ActionResult<WeatherForecast> Delete(int id) | |
| { | |
| var result = _forecastDbService.Delete(id); | |
| if (result > 0) | |
| return NoContent(); | |
| else | |
| return NotFound(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment