Created
December 9, 2019 18:14
-
-
Save jrconner384/ded925b57e53248217e2be6fe816970b to your computer and use it in GitHub Desktop.
Connect to a MongoDB to pull out CCG card info and fit it to a JSON array.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Threading.Tasks; | |
using Microsoft.AspNetCore.Mvc; | |
using Microsoft.Azure.WebJobs; | |
using Microsoft.Azure.WebJobs.Extensions.Http; | |
using Microsoft.AspNetCore.Http; | |
using Microsoft.Extensions.Logging; | |
using MongoDB.Driver; | |
using MongoDB.Bson; | |
using System; | |
namespace Cardalog.Api | |
{ | |
public static class ReadCards | |
{ | |
[FunctionName("ReadCards")] | |
public static async Task<IActionResult> Run( | |
[HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "cards")] HttpRequest req, | |
ILogger log) | |
{ | |
try | |
{ | |
var client = new MongoClient("mongodb://127.0.0.1:27017"); | |
var db = client.GetDatabase("cardalog"); | |
var coll = db.GetCollection<BsonDocument>("cards"); | |
var projection = Builders<BsonDocument>.Projection.Exclude("_id"); | |
var cardsBson = await coll.Find(new BsonDocument()).Project(projection).ToListAsync(); | |
var cards = new BsonArray(cardsBson); | |
return (ActionResult)new OkObjectResult(cards.ToJson()); | |
} | |
catch (Exception e) | |
{ | |
log.LogError(e, "Something went wrong"); | |
return new BadRequestObjectResult("Something went wrong."); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment