Skip to content

Instantly share code, notes, and snippets.

@csehammad
Created December 16, 2020 22:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save csehammad/14ed7590d3943304176d0e2ccbbfea72 to your computer and use it in GitHub Desktop.
Save csehammad/14ed7590d3943304176d0e2ccbbfea72 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Grpc.Core;
using StockServices.Protos;
namespace StockServices.Services
{
public class StockService : Stock.StockBase
{
private static List<Product> _allProducts = new List<Product>();
public override Task<Result> AddProduct(Product request, ServerCallContext context)
{
if (string.IsNullOrEmpty(request.Name))
return Task.FromResult<Result>(new Result { Msg = "Product Name Can't be nulled", Status = false });
if (_allProducts.FirstOrDefault(f => f.Code == request.Code) != null)
return Task.FromResult<Result>(new Result { Msg = "Product is already Added", Status = false });
_allProducts.Add(request);
return Task.FromResult<Result>(new Result { Msg = "Added. Total Products: "+_allProducts.Count.ToString(), Status = true });
}
public override async Task GetAllProducts(Empty request, IServerStreamWriter<Product> responseStream, ServerCallContext context)
{
foreach (var each in _allProducts)
{
await responseStream.WriteAsync(each);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment