C#介面配置

昕仒發表於2024-09-14
點選檢視程式碼
[Route("Values/Matches")]
[HttpGet]
public IHttpActionResult Matches(string key)
{
    var matches = db.Event.Where(a => a.StatusId == 2).First().Match.Where(a => a.TeamA != null && a.TeamB != null).ToList().Select(a => new 
    {
        TeamA = new
        {
            a.Team.Name,
            a.Team.Logo,
            Score = a.MatchScore.Sum(b => b.TeamAScore)
        },
        TeamB = new
        {
            a.Team1.Name,
            a.Team1.Logo,
            Score = a.MatchScore.Sum(b => b.TeamBScore)
        },
        a.Stadium.Name,
        Date = $"{a.MatchDate:yyyy-MM-dd} {a.StartTime:hh\\:mm}"
    });
    if (key != null)
    {
        if (DateTime.TryParseExact(key, "yyyy-MM-dd HH:mm", CultureInfo.InvariantCulture, DateTimeStyles.None, out date))
        {
            matches = matches.Where(a => a.Date.Equals(key)).ToList();
        }
        else
        {
            matches = matches.Where(a => a.TeamA.Name.Contains(key) || a.TeamB.Name.Contains(key) || a.Name.Contains(key)).ToList();
        }
    }
    return Json(matches);
}

點選檢視程式碼
[Route("Values/Events")]
[HttpGet]
public IHttpActionResult Events()
{
    var Events = db.Event.Select(a => new
    {
        a.Id,
        a.Name,
        Type = a.EventType.Name,
        a.StatusId
    }).ToList();
    return Json(new
    {
        Events,
        Current = Events.FindIndex(a => a.StatusId == 2),
    });
}

點選檢視程式碼
[Route("Values/Teams")]
[HttpGet]
public IHttpActionResult Teams(int Id)
{
    var e = db.Event.Single(a => a.Id == Id);
    var teams = e.TeamInEvent.Select(a => new TeamItem
    {
        Id = a.TeamId,
        Name = a.Team.Name,
        Logo = a.Team.Logo,
        Score = 0
    }).ToList();
    var matches = e.Match.Where(a => a.TeamA != null && a.TeamB != null).Select(a => new
    {
        ScoreA = a.MatchScore.Sum(b => b.TeamAScore),
        ScoreB = a.MatchScore.Sum(b => b.TeamBScore),
        IdA = a.TeamA,
        IdB = a.TeamB
    }).ToList();
    foreach (var match in matches)
    {
        var ta = teams.Find(a => a.Id == match.IdA);
        var tb = teams.Find(a => a.Id == match.IdB);
        if (match.ScoreA > match.ScoreB)
        {
            ta.Score += 2;
            tb.Score += 1;
            ta.WinScore = match.ScoreA - match.ScoreB;
        }
        else if (match.ScoreA < match.ScoreB)
        {
            tb.Score += 2;
            ta.Score += 1;
            tb.WinScore = match.ScoreB - match.ScoreA;
        }
    }
    return Json(new
    {
        Teams = teams.OrderByDescending(a => a.Score).ThenByDescending(a => a.WinScore).ThenBy(a => a.Name).ToList(),
        Ongoing = e.StatusId == 2
    });
}
public class TeamItem
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Logo { get; set; }
    public int Score { get; set; }
    public int WinScore { get; set; }
}

相關文章