37 lines
1.0 KiB
C#
37 lines
1.0 KiB
C#
|
using System;
|
||
|
using System.Collections.Generic;
|
||
|
using System.Linq;
|
||
|
using System.Threading.Tasks;
|
||
|
using Microsoft.AspNetCore.Mvc;
|
||
|
using Microsoft.Extensions.Logging;
|
||
|
using usaquote_api.Data;
|
||
|
using usaquote_api.Models;
|
||
|
|
||
|
namespace usaquote_api.Controllers{
|
||
|
[ApiController]
|
||
|
[Route("[controller]")]
|
||
|
public class QuoteController : ControllerBase {
|
||
|
private readonly UsaquoteContext _context;
|
||
|
public QuoteController(UsaquoteContext context) {
|
||
|
_context = context;
|
||
|
}
|
||
|
|
||
|
[HttpGet]
|
||
|
public async Task<ActionResult<Quote>> GetQuote() {
|
||
|
int count = _context.Quotes.Count();
|
||
|
var rnd = new Random();
|
||
|
var quote = await _context.Quotes.FindAsync(rnd.Next(count));
|
||
|
return quote;
|
||
|
}
|
||
|
[HttpGet("{speaker}")]
|
||
|
public async Task<ActionResult<Quote>> GetBySpeaker(string speaker) {
|
||
|
var rnd = new Random();
|
||
|
var quotelist = _context.Quotes.Where(q => q.speaker == speaker).ToList<Quote>();
|
||
|
var quote = quotelist[rnd.Next(quotelist.Count())];
|
||
|
// var quote = quotelist.First();
|
||
|
// var x = rnd.Next(quotelist.Count());
|
||
|
return quote;
|
||
|
}
|
||
|
|
||
|
}
|
||
|
}
|