flower-story/Server/Controller/UserController.cs
2023-05-23 08:37:20 +08:00

61 lines
1.8 KiB
C#

using Blahblah.FlowerStory.Server.Data;
using Blahblah.FlowerStory.Server.Data.Model;
using Microsoft.AspNetCore.Mvc;
namespace Blahblah.FlowerStory.Server.Controller
{
[ApiController]
[Route("users")]
public class UserController : ControllerBase
{
private readonly FlowerDatabase database;
private readonly ILogger<UserController> logger;
public UserController(FlowerDatabase db, ILogger<UserController> logger)
{
database = db;
this.logger = logger;
}
[Route("query")]
[HttpGet]
public ActionResult<UserItem[]> GetUsers()
{
//var forecast = Enumerable.Range(1, 5).Select(index =>
// new WeatherForecast
// {
// Date = DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
// TemperatureC = Random.Shared.Next(-20, 55),
// Summary = summaries[Random.Shared.Next(summaries.Length)]
// })
// .ToArray();
//return Ok(forecast);
return Ok(database.Users.ToArray());
}
[Route("update")]
[HttpPost]
public ActionResult<int> UpdateUser([FromBody] UserItem item)
{
if (item.Id > 0)
{
database.Update(item);
}
else
{
database.Add(item);
}
var count = database.SaveChanges();
if (count > 0)
{
logger.LogInformation("{number} of entries written to database.", count);
}
else
{
logger.LogWarning("no data written to database.");
}
return Ok(item.Id);
}
}
}