Untitled
unknown
plain_text
a year ago
8.7 kB
4
Indexable
Controller using Microsoft.AspNetCore.Mvc; using ProductStockMaintenance.Repo; namespace ProductStockMaintenance.Controllers; [ApiController] public class LoginController:ControllerBase { private readonly IRepo _repository; public LoginController(IRepo repo) { _repository = repo; } [HttpPost("/ValidateLoginCredentials")] public async Task<IActionResult> Post(string username,[FromHeader]string password){ try { var result = await _repository.ValidateUser(username,password); if(result=="Admin") { return Ok("AdminLogin") ; } else if(result=="user") { return Ok("userLogin") ; } else { return Ok("not a user"); } } catch (Exception) { return Ok("Give loginCredentials"); } } } Test // ```csharp using Microsoft.AspNetCore.Mvc; using Moq; using NUnit.Framework; using Product_Stock_Maintenance.models; using ProductStockMaintenance.Controllers; using ProductStockMaintenance.Repo; namespace UnitTestProject1 { [TestFixture] public class UnitTest1 { [Test] [TestCase("pavan","pavan@123")] [TestCase("sjfa","sdfg")] [TestCase("bharadwaj","bharadwaj@123")] public async Task Test(string Username,string Password) { //Arrange var mockrepo = new Mock<IRepo>(); var vaildUser = new Login {Username = "bharadwaj" , Password = "bharadwaj@123"}; var vaildAdmin = new Login {Username = "pavan" , Password = "pavan@123"}; mockrepo.Setup(repo => repo.ValidateUser(vaildAdmin.Username,vaildAdmin.Password)).ReturnsAsync("Admin"); mockrepo.Setup(repo => repo.ValidateUser(vaildUser.Username,vaildUser.Password)).ReturnsAsync("user"); //Act var LoginController = new LoginController(mockrepo.Object); var temp = await LoginController.Post(Username,Password); var OkResult = (OkObjectResult)temp; var Result = OkResult.Value as string; //Assert switch(Result){ case "AdminLogin": case "userLogin": case "not a user": Assert.Pass(); break; default: Assert.Fail(); break; } } } } // ``` repo using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using Product_Stock_Maintenance.models; namespace ProductStockMaintenance.Repo { public class Repo:IRepo { private readonly TemplateContext _context; public Repo(TemplateContext db) { _context = db; } public async Task<string> ValidateUser(string username,[FromHeader]string password) { List<Login> users = new List<Login>(); try { users = await (from data in _context.Logins where data.Username==username && data.Password==password select data).ToListAsync(); foreach(Login data in users) { if(data.AdminAccess==1) { return "Admin"; } else if(data.AdminAccess==0) { return "user"; } else { return "not a user"; } } return "sucessfully completed"; } catch (Exception) { return "Give loginCredentials"; } } public async Task<Object> Products() { try { var inventory =await(from i in _context.Inventories from c in _context.Categorynames where i.CategoryId==c.CategoryId && i.IsDelete==1 select new {i.ProductId,i.ProductName,i.Price,i.Quantity,c.Category,i.ProductImage} ).ToListAsync(); return inventory; } catch (Exception) { throw; } } public async Task<object> AddQuantity(int Product_Id, int quantity) { try { var res = _context.Inventories.Where(x => x.ProductId == Product_Id) ; res.ExecuteUpdate(setters => setters.SetProperty(x => x.Quantity, x => x.Quantity + quantity)); await _context.SaveChangesAsync(); bool var=res.Any(); if(var) { return res; } else{ return "product not existed in the inevntory"; } } catch(Exception) { throw; } } public async Task<object> DeleteProduct(int productId) { try { _context.Database.EnsureCreated(); var delete = await (from s in _context.Inventories where s.ProductId==productId select s).ToListAsync(); if(_context.Inventories.Any(s=>s.IsDelete==0)){ return "Invalid Product or In inventory contains more than one product"; } else{ foreach(Inventory it in delete){ it.IsDelete=0; await _context.SaveChangesAsync(); } return delete; } } catch(Exception) { throw; } } public async Task<object> AddNewProducts(Inventory inventory) { try { _context.Database.EnsureCreated(); inventory.IsDelete=1; var newobject= await(from s in _context.Inventories from c in _context.Categorynames where s.CategoryId==c.CategoryId && s.IsDelete==1 select new {s.ProductName,s.Quantity,s.Price,c.CategoryId,s.CreatedBy,s.ProductImage}).ToListAsync(); await _context.Inventories.AddAsync(inventory); _context.SaveChanges(); return newobject; } catch (Exception) { return "Dupliate Entry"; } } public async Task<object> GetByProduct(string category) { try { var inv =await(from i in _context.Inventories join c in _context.Categorynames on i.CategoryId equals c.CategoryId where c.Category.Contains(category) select new { i.ProductId,i.ProductName,i.Quantity,i.Price}).ToListAsync(); bool res=inv.Any(); if(res) { return inv; } else{ return "enter a valid product in the inventory"; } } catch (Exception ex) { return (ex.Message); } } public async Task<object> UserQuantity(int productID ,int quantity ) { try{ var res = _context.Inventories.Where(x => x.ProductId == productID) ; res.ExecuteUpdate(setters => setters.SetProperty(x => x.Quantity, x => x.Quantity - quantity)); await _context.SaveChangesAsync(); return res; } catch(Exception) { throw; } } public async Task<object> Search(string product) { try { var inv =await(from i in _context.Inventories join c in _context.Categorynames on i.CategoryId equals c.CategoryId where c.Category.Contains(product)||i.ProductName.Contains(product) select new { i.ProductId,i.ProductName,i.Quantity,i.Price}).ToListAsync(); bool res=inv.Any(); if(res) { return inv; } else{ return "enter a valid product in the inventory"; } } catch (Exception ex) { return (ex.Message); } } } }
Editor is loading...
Leave a Comment