Newer
Older
NoteApi / Controller / HololiveScheduleController.cs
@fabre fabre 13 days ago 1 KB init
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.OData.Routing.Controllers;
using Microsoft.AspNetCore.OData.Query;
using Microsoft.EntityFrameworkCore;
using Microsoft.AspNetCore.OData.Results;


public class HololiveScheduleController : ODataController
{
    private readonly AppDbContext _db;
    public HololiveScheduleController(AppDbContext db) => _db = db;

    // GET /odata/HololiveSchedule
    [EnableQuery]
    public IQueryable<HololiveSchedule> Get()
        => _db.HololiveSchedule;

    // GET /odata/HololiveSchedule('{id}')
    [EnableQuery]
    public SingleResult<HololiveSchedule> Get([FromRoute] Guid key)
        => SingleResult.Create(_db.HololiveSchedule.Where(x => x.Id == key));

    // POST /odata/HololiveSchedule
    public async Task<IActionResult> Post([FromBody] HololiveSchedule HololiveSchedule)
    {
        HololiveSchedule.Id = Guid.NewGuid();

        _db.HololiveSchedule.Add(HololiveSchedule);
        await _db.SaveChangesAsync();

        return Created(HololiveSchedule);
    }

    // PUT /odata/HololiveSchedule('{id}')
    public async Task<IActionResult> Put([FromRoute] Guid key, [FromBody] HololiveSchedule update)
    {
        var existing = await _db.HololiveSchedule.FindAsync(key);
        if (existing == null)
            return NotFound();

        existing.StartDt = update.StartDt;
        existing.MemberName = update.MemberName;
        existing.StreamUrl = update.StreamUrl;
        existing.StreamTitle = update.StreamTitle;
        existing.StreamImage = update.StreamImage;
        existing.Md5 = update.Md5;

        await _db.SaveChangesAsync();
        return Updated(existing);
    }

    // DELETE /odata/HololiveSchedule('{id}')
    public async Task<IActionResult> Delete([FromRoute] Guid key)
    {
        var existing = await _db.HololiveSchedule.FindAsync(key);
        if (existing == null)
            return NotFound();

        _db.HololiveSchedule.Remove(existing);
        await _db.SaveChangesAsync();
        return NoContent();
    }
}