using Microsoft.Graph; using Microsoft.IdentityModel.Tokens; using rgbc_sds.dal.DB; using rgbc_sds.services.Models; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace rgbc_sds.services.Services { public class SupplierService { #region [ Variables ] private readonly rgbc_sdsContext _context; //rgbc_sdsContext _context = new rgbc_sdsContext(); #endregion [ Variables ] #region [ Constructor ] public SupplierService(rgbc_sdsContext context) { _context = context; } #endregion [ Constructor ] public SupplierModel GetSupplierList() { var model = new SupplierModel(); var supplierList = _context.sdsSupplier.OrderBy(x => x.name).ToList(); foreach (var supplier in supplierList) { model.SupplierListModel.Add(new SupplierModel { Id = supplier.id, Name = supplier.name, IsActive = supplier.is_active, SupplierStatus = supplier.is_active == true ? "Yes" : "No", UpdatedBy = !supplier.updated_by.IsNullOrEmpty() ? supplier.updated_by : supplier.created_by, EditedAt = supplier.updated_at != null ? supplier.updated_at.Value.ToString("yyyy/MM/dd HH:mm") : supplier.created_at.Value.ToString("yyyy/MM/dd HH:mm") }); } return model; } public SupplierModel FilterSupplierList(string searchString, int isActive) { var returnModel = new SupplierModel(); if (!searchString.IsNullOrEmpty() && isActive == 0) { var supplierList = _context.sdsSupplier.Where(x => x.name.Contains(searchString)).OrderBy(x => x.name).ToList(); foreach (var supplier in supplierList) { returnModel.SupplierListModel.Add(new SupplierModel { Id = supplier.id, Name = supplier.name, IsActive = supplier.is_active, SupplierStatus = supplier.is_active == true ? "Yes" : "No", UpdatedBy = !supplier.updated_by.IsNullOrEmpty() ? supplier.updated_by : supplier.created_by, EditedAt = supplier.updated_at != null ? supplier.updated_at.Value.ToString("yyyy/MM/dd HH:mm") : supplier.created_at.Value.ToString("yyyy/MM/dd HH:mm") }); } } else if (!searchString.IsNullOrEmpty() && isActive > 0) { if (isActive == 1) { var supplierList = _context.sdsSupplier.Where(x => x.name.Contains(searchString) && x.is_active == true).OrderBy(x => x.name).ToList(); foreach (var supplier in supplierList) { returnModel.SupplierListModel.Add(new SupplierModel { Id = supplier.id, Name = supplier.name, IsActive = supplier.is_active, SupplierStatus = supplier.is_active == true ? "Yes" : "No", UpdatedBy = !supplier.updated_by.IsNullOrEmpty() ? supplier.updated_by : supplier.created_by, EditedAt = supplier.updated_at != null ? supplier.updated_at.Value.ToString("yyyy/MM/dd HH:mm") : supplier.created_at.Value.ToString("yyyy/MM/dd HH:mm") }); } } else if (isActive == 2) { var supplierList = _context.sdsSupplier.Where(x => x.name.Contains(searchString) && x.is_active == false).OrderBy(x => x.name).ToList(); foreach (var supplier in supplierList) { returnModel.SupplierListModel.Add(new SupplierModel { Id = supplier.id, Name = supplier.name, IsActive = supplier.is_active, SupplierStatus = supplier.is_active == true ? "Yes" : "No", UpdatedBy = !supplier.updated_by.IsNullOrEmpty() ? supplier.updated_by : supplier.created_by, EditedAt = supplier.updated_at != null ? supplier.updated_at.Value.ToString("yyyy/MM/dd HH:mm") : supplier.created_at.Value.ToString("yyyy/MM/dd HH:mm") }); } } } else if (searchString.IsNullOrEmpty() && isActive > 0) { if (isActive == 1) { var supplierList = _context.sdsSupplier.Where(x => x.is_active == true).OrderBy(x => x.name).ToList(); foreach (var supplier in supplierList) { returnModel.SupplierListModel.Add(new SupplierModel { Id = supplier.id, Name = supplier.name, IsActive = supplier.is_active, SupplierStatus = supplier.is_active == true ? "Yes" : "No", UpdatedBy = !supplier.updated_by.IsNullOrEmpty() ? supplier.updated_by : supplier.created_by, EditedAt = supplier.updated_at != null ? supplier.updated_at.Value.ToString("yyyy/MM/dd HH:mm") : supplier.created_at.Value.ToString("yyyy/MM/dd HH:mm") }); } } else if (isActive == 2) { var supplierList = _context.sdsSupplier.Where(x => x.is_active == false).OrderBy(x => x.name).ToList(); foreach (var supplier in supplierList) { returnModel.SupplierListModel.Add(new SupplierModel { Id = supplier.id, Name = supplier.name, IsActive = supplier.is_active, SupplierStatus = supplier.is_active == true ? "Yes" : "No", UpdatedBy = !supplier.updated_by.IsNullOrEmpty() ? supplier.updated_by : supplier.created_by, EditedAt = supplier.updated_at != null ? supplier.updated_at.Value.ToString("yyyy/MM/dd HH:mm") : supplier.created_at.Value.ToString("yyyy/MM/dd HH:mm") }); } } } else { var supplierList = _context.sdsSupplier.OrderBy(x => x.name).ToList(); foreach (var supplier in supplierList) { returnModel.SupplierListModel.Add(new SupplierModel { Id = supplier.id, Name = supplier.name, IsActive = supplier.is_active, SupplierStatus = supplier.is_active == true ? "Yes" : "No", UpdatedBy = !supplier.updated_by.IsNullOrEmpty() ? supplier.updated_by : supplier.created_by, EditedAt = supplier.updated_at != null ? supplier.updated_at.Value.ToString("yyyy/MM/dd HH:mm") : supplier.created_at.Value.ToString("yyyy/MM/dd HH:mm") }); } } return returnModel; } public SupplierModel GetSupplier(int id) { var returnModel = new SupplierModel(); var supplier = _context.sdsSupplier.Where(x => x.id == id).FirstOrDefault(); if (supplier != null) { returnModel.Id = supplier.id; returnModel.Name = supplier.name; returnModel.IsActive = supplier.is_active; returnModel.CreatedBy = supplier.created_by; returnModel.CreatedAt = supplier.created_at; returnModel.UpdatedBy = supplier.updated_by; returnModel.UpdatedAt = supplier.updated_at; } return returnModel; } public string AddSupplier(SupplierModel model) { var insertModel = new sdsSupplier() { name = model.Name, created_by = model.CreatedBy, created_at = model.CreatedAt }; try { _context.sdsSupplier.Add(insertModel); _context.SaveChanges(); return "success"; } catch (Exception ex) { return ex.Message; } } public SupplierModel UpdateSupplier(SupplierModel model) { var existingSupplier = _context.sdsSupplier.Where(x => x.id == model.Id).FirstOrDefault(); if (existingSupplier != null) { existingSupplier.name = model.Name; existingSupplier.is_active = model.IsActive; existingSupplier.updated_by = model.UpdatedBy; existingSupplier.updated_at = model.UpdatedAt; try { _context.sdsSupplier.Update(existingSupplier); _context.SaveChanges(); } catch (Exception) { throw; } } return model; } public bool DeleteSupplier(int Id, string loggedInUserName) { var existingSupplier = _context.sdsSupplier.Where(x => x.id == Id && x.is_active == true).FirstOrDefault(); if (existingSupplier != null) { existingSupplier.is_active = false; existingSupplier.updated_by = loggedInUserName; existingSupplier.updated_at = DateTime.Now; try { _context.sdsSupplier.Update(existingSupplier); _context.SaveChanges(); return true; } catch (Exception) { return false; } } return false; } } }