Skip to content

Instantly share code, notes, and snippets.

@georgekosmidis
Last active October 29, 2021 09:53
Show Gist options
  • Select an option

  • Save georgekosmidis/930b66ba69e186c833355d370ecf4b75 to your computer and use it in GitHub Desktop.

Select an option

Save georgekosmidis/930b66ba69e186c833355d370ecf4b75 to your computer and use it in GitHub Desktop.
"LiteDbOptions": {
"DatabaseLocation": "LiteDb/LiteDbTest.db"
}
dotnet new webapi
dotnet add package LiteDB
dotnet build
public class LiteDbContext : ILiteDbContext
{
public LiteDatabase Database { get; }
public LiteDbContext(IOptions<LiteDbOptions> options)
{
Database = new LiteDatabase(options.Value.DatabaseLocation);
}
}
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);
}
}
services.AddSingleton<ILiteDbContext, LiteDbContext>();
services.AddTransient<ILiteDbWeatherForecastService, LiteDbWeatherForecastService>();
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; }
}
[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