Untitled
unknown
plain_text
2 years ago
13 kB
11
Indexable
Template Context using System; using System.Collections.Generic; using Microsoft.EntityFrameworkCore; namespace Product_Stock_Maintenance.models; public partial class TemplateContext : DbContext { public TemplateContext() { } public TemplateContext(DbContextOptions<TemplateContext> options) : base(options) { } public virtual DbSet<Categoryname> Categorynames { get; set; } public virtual DbSet<Inventory> Inventories { get; set; } public virtual DbSet<Login> Logins { get; set; } public virtual DbSet<Logincheck> Loginchecks { get; set; } protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder .UseCollation("utf8mb4_0900_ai_ci") .HasCharSet("utf8mb4"); modelBuilder.Entity<Categoryname>(entity => { entity.HasKey(e => e.CategoryId).HasName("PRIMARY"); entity.ToTable("categoryname"); entity.HasIndex(e => e.Category, "category_UNIQUE").IsUnique(); entity.Property(e => e.CategoryId) .ValueGeneratedNever() .HasColumnName("categoryID"); entity.Property(e => e.Category) .HasMaxLength(45) .HasColumnName("category"); }); modelBuilder.Entity<Inventory>(entity => { entity.HasKey(e => e.ProductId).HasName("PRIMARY"); entity.ToTable("inventory"); entity.HasIndex(e => e.CategoryId, "FK_catble"); entity.HasIndex(e => e.ProductName, "productName_UNIQUE").IsUnique(); entity.Property(e => e.ProductId) .ValueGeneratedNever() .HasColumnName("productId"); entity.Property(e => e.CategoryId).HasColumnName("categoryID"); entity.Property(e => e.CreatedBy) .HasMaxLength(45) .HasColumnName("createdBy"); entity.Property(e => e.CreatedDate).HasDefaultValueSql("curdate()"); entity.Property(e => e.Price).HasColumnName("price"); entity.Property(e => e.ProductImage).HasMaxLength(10000); entity.Property(e => e.ProductName) .HasMaxLength(45) .HasColumnName("productName"); entity.Property(e => e.Quantity).HasColumnName("quantity"); entity.HasOne(d => d.Category).WithMany(p => p.Inventories) .HasForeignKey(d => d.CategoryId) .HasConstraintName("FK_catble"); }); modelBuilder.Entity<Login>(entity => { entity.HasKey(e => e.AdminId).HasName("PRIMARY"); entity.ToTable("login"); entity.Property(e => e.AdminId) .ValueGeneratedNever() .HasColumnName("adminId"); entity.Property(e => e.Password) .HasMaxLength(45) .HasColumnName("password"); entity.Property(e => e.Username) .HasMaxLength(45) .HasColumnName("username"); }); modelBuilder.Entity<Logincheck>(entity => { entity .HasNoKey() .ToTable("logincheck"); entity.Property(e => e.Password).HasMaxLength(45); entity.Property(e => e.UserName).HasMaxLength(45); }); OnModelCreatingPartial(modelBuilder); } partial void OnModelCreatingPartial(ModelBuilder modelBuilder); } 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.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; } } } } Nunit test case using Microsoft.AspNetCore.Mvc; using Moq; using NUnit.Framework; using Product_Stock_Maintenance.models; using ProductStockMaintenance.Controllers; using ProductStockMaintenance.Repo; namespace UnitTest { [TestFixture] public class RepoTest { private Repo Repo; private Mock<TemplateContext> mockrepo; [SetUp] public void setup(){ var mockrepo = new Mock<TemplateContext>(); Repo = new Repo(mockrepo.Object); } [Test] public async Task Test(){ try{ var vaildUser = new Login {AdminId = 1 ,Username = "bharadwaj" , Password = "bharadwaj@123", AdminAccess = true}; var vaildAdmin = new Login {AdminId = 2,Username = "pavan" , Password = "pavan@123" , AdminAccess = true }; var invalidLogin = new Login { AdminId = 3, Username = "1234", Password = "1234@123", AdminAccess = false }; mockrepo.Setup(x => x.Logins.FirstOrDefaultAsync(y => y.Username == "pavan" && y.Password == "pavan@123" )).ReturnsAsync("Admin"); var Result = await Repo.ValidateUser(vaildAdmin.Username,vaildAdmin.Password); switch(Result){ case "Admin": Assert.Pass(); break; default: Assert.Fail(); break; } } catch(Exception){ Assert.Fail(); } } } }
Editor is loading...
Leave a Comment