Untitled
unknown
plain_text
2 years ago
8.7 kB
6
Indexable
Repo 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,string password) { try { var users = await (from data in _context.Logins where data.Username==username && data.Password==password select data).FirstOrDefaultAsync(); if(users != null ) { return (bool)users.AdminAccess ? "admin" : "users"; } else { return "Invalid login Credentials "; } } catch (Exception ex) { throw new Exception("error occured",ex); } } 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==true select new {i.ProductId,i.ProductName,i.Price,i.Quantity,c.Category,i.ProductImage} ).ToListAsync(); return inventory; } catch (Exception ex) { throw new Exception("No product Found ",ex); } } 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)); bool var=res.Any(); if(var) { await _context.SaveChangesAsync(); return res; } return "product not existed in the inevntory"; } catch(Exception ex) { throw new Exception(" " ,ex); } } public async Task<object> DeleteProduct(int productId) { try { _context.Database.EnsureCreated(); var delete = await (from s in _context.Inventories where s.ProductId == productId && s.IsDelete==true select s).FirstOrDefaultAsync(); if (delete != null) { delete.IsDelete = false; await _context.SaveChangesAsync(); return delete; } return "Invalid ProductId OR This product already deleted"; } catch(Exception ex) { throw new Exception(" " ,ex); } } public async Task<object> AddNewProducts(Inventory inventory) { try { _context.Database.EnsureCreated(); inventory.IsDelete=true; var newobject= await(from s in _context.Inventories select new{ s.ProductName,s.Quantity,s.Price,s.CreatedBy,s.CategoryId}).ToListAsync(); if (newobject.Any(obj =>obj.ProductName==inventory.ProductName)) { return "Dupliate Entry"; } _context.Inventories.Add(inventory); _context.SaveChanges(); return inventory; } catch(Exception ex) { throw new Exception(" " ,ex); } } public async Task<object> GetProductsInCategory(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; } 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) ; var a = _context.Inventories.Where(x=>x.ProductId==productID).ToList(); foreach(var item in a) { if(item.Quantity>=quantity) { res.ExecuteUpdate(setters => setters.SetProperty(x => x.Quantity, x => x.Quantity - quantity)); await _context.SaveChangesAsync(); return res; } } return "Selected greater than available quantity"; } catch(Exception ex) { throw new Exception(" " ,ex); } } 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); } } public async Task<object> ActivateProduct(int productID) { try { _context.Database.EnsureCreated(); var delete = await (from s in _context.Inventories where s.ProductId == productID && s.IsDelete==false select s).FirstOrDefaultAsync(); if (delete != null) { delete.IsDelete = true; await _context.SaveChangesAsync(); return delete; } return "Invalid ProductId OR This product already deleted"; } catch(Exception ex) { throw new Exception(" " ,ex); } } public async Task<object> Category() { try { var category= await (from s in _context.Categorynames select new{ s.Category}).ToListAsync(); return category; } catch(Exception ) { throw; } } } } Controller using Microsoft.AspNetCore.Mvc; using Product_Stock_Maintenance.models; 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([FromBody] Logincheck logincheck){ try { var result = await _repository.ValidateUser(logincheck.UserName,logincheck.Password); return Ok(result); } catch (Exception) { throw ; } } }
Editor is loading...
Leave a Comment