Skip to content

Instantly share code, notes, and snippets.

@ranfysvalle02
Last active February 1, 2023 17:26
Show Gist options
  • Save ranfysvalle02/542b7e4f53eb7ff063940b430282d8cd to your computer and use it in GitHub Desktop.
Save ranfysvalle02/542b7e4f53eb7ff063940b430282d8cd to your computer and use it in GitHub Desktop.
DOTNETDEMO-TXController-API
using Microsoft.AspNetCore.Mvc;
using MongoDB.Driver;
using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes;
using System.Net;
using System;
namespace dotnetdemo.Controllers;
[BsonIgnoreExtraElements]
public class TxDetails
{
public int amount { get; set; }
public String total { get; set; }
public String price { get; set; }
public String symbol { get; set; }
public String transaction_code { get; set; }
public DateTime date { get; set; }
}
[BsonIgnoreExtraElements]
public class Tx
{
[BsonId]
public ObjectId Id { get; set; }
public int account_id { get; set; }
public int transaction_count { get; set; }
public DateTime bucket_start_date { get; set; }
public DateTime bucket_end_date { get; set; }
public TxDetails[] transactions { get; set; }
}
[ApiController]
[Route("[controller]")]
public class TxController : ControllerBase
{
[HttpGet(Name = "GetMongoDB")]
public IEnumerable<Tx> Get()
{
var settings = MongoClientSettings.FromConnectionString("mongodb+srv://testRFV:[email protected]/?retryWrites=true&w=majority");
settings.ServerApi = new ServerApi(ServerApiVersion.V1);
var client = new MongoClient(settings);
var database = client.GetDatabase("sample_analytics");
var collection = database.GetCollection<Tx>("transactions");
var filter = new BsonDocument();
var filterRawBSON = BsonDocument.Parse("{ }");
var filterByAccount = Builders<Tx>.Filter.Eq("account_id", 123);
var x = collection.Find(filterByAccount).Limit(10).ToListAsync();
return x.Result.ToArray();
}
[HttpPost(Name = "InsertRandomDocument")]
public bool Insert()
{
var settings = MongoClientSettings.FromConnectionString("mongodb+srv://testRFV:[email protected]/?retryWrites=true&w=majority");
settings.ServerApi = new ServerApi(ServerApiVersion.V1);
var client = new MongoClient(settings);
var database = client.GetDatabase("sample_analytics");
var collection = database.GetCollection<Tx>("transactions");
var filter = new BsonDocument();
var d1 = DateTime.Now.AddDays(-1);
var d2 = DateTime.Now.AddDays(1);
Random rnd = new Random();
var p = rnd.Next(1, 100);
var filterByAccount = Builders<Tx>.Update.Set("account_id", 123)
.Inc("transaction_count",1)
.Set("bucket_start_date", d1)
.Set("bucket_end_date", d2)
.Push("transactions", new TxDetails{
amount = p,
total = p.ToString(),
price = "$"+p.ToString(),
symbol = "ABC-AUTO",
transaction_code = "buy",
date = DateTime.Now
});
var updateOptions = new UpdateOptions() { IsUpsert = true };
var x = collection.UpdateOne(Builders<Tx>.Filter.Eq("account_id", 123), filterByAccount,updateOptions);
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment