Advertisement
Guest User

Untitled

a guest
Jun 20th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.17 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. using Crud.AllocationConfigurations;
  6. using Crud.AllocationConfigurations.AttributeColumnsSettings;
  7. using Crud.AllocationConfigurations.Models;
  8. using Microsoft.EntityFrameworkCore;
  9. using Web.Api.Core.Crud.Employees;
  10. using Web.Api.Infrastructure.EF.SqlServer.Context;
  11. using WebApi;
  12.  
  13. namespace EF.SqlServer.Repositories
  14. {
  15.     public class AllocationConfigurationRepository : IAllocationConfigurationRepository
  16.     {
  17.         private readonly GiacomoContiContext _context;
  18.         private readonly int loggedUserId = 7; //TODO: Fixed when user login finished
  19.         private readonly int defaultUser = 12; // From where?
  20.         public AllocationConfigurationRepository(GiacomoContiContext context)
  21.         {
  22.             _context = context;
  23.         }
  24.  
  25.         public IEnumerable<StoresColumnSetting> GetStoreColumnsSettings()
  26.         {
  27.             var visibleAndOrderSettings = _context.StoreColumnsSettings
  28.                 .Where(u => u.EmployeeId == loggedUserId)
  29.                 .Select(x => new StoresColumnSetting()
  30.                 {
  31.                     Id = x.Id,
  32.                     InternalName = x.InternalName,
  33.                     Order = x.ColumnOrder,
  34.                     IsVisible = x.IsVisible
  35.                 })
  36.                 .ToList();
  37.  
  38.             if (!visibleAndOrderSettings.Any())
  39.             {
  40.                 visibleAndOrderSettings = _context.StoreColumnsSettings
  41.                     .Where(e => e.EmployeeId == defaultUser)
  42.                     .Select(x => new StoresColumnSetting()
  43.                     {
  44.                         Id = x.Id,
  45.                         InternalName = x.InternalName,
  46.                         Order = x.ColumnOrder,
  47.                         IsVisible = x.IsVisible
  48.                     })
  49.                     .ToList();
  50.             }
  51.  
  52.             return visibleAndOrderSettings;
  53.         }
  54.         public async Task UpdateStoreColumnsSettings(List<StoresColumnSetting> storesColumnsSettingsList)
  55.         {
  56.             var employeeSettings = _context.StoreColumnsSettings
  57.                 .Where(e => e.EmployeeId == loggedUserId)
  58.                 .ToList();
  59.                
  60.             if (employeeSettings.Any())
  61.             {
  62.                 foreach (var item in storesColumnsSettingsList)
  63.                 {
  64.                     var dbExistingSetting = _context.StoreColumnsSettings
  65.                         .Where(s => s.Id == item.Id)
  66.                         .FirstOrDefault();
  67.                     if (dbExistingSetting != null)
  68.                     {
  69.                         dbExistingSetting.ColumnOrder = item.Order;
  70.                         dbExistingSetting.IsVisible = item.IsVisible;
  71.                     }
  72.                     _context.Entry(dbExistingSetting).State = EntityState.Modified;
  73.                 }
  74.             }
  75.             else
  76.             {
  77.                 foreach (var item in storesColumnsSettingsList)
  78.                 {
  79.                     var newSetting = new StoreColumnsSettings()
  80.                     {
  81.                         InternalName = item.InternalName,
  82.                         ColumnOrder = item.Order,
  83.                         IsVisible = item.IsVisible,
  84.                         EmployeeId = loggedUserId
  85.                     };
  86.                     _context.StoreColumnsSettings.Add(newSetting);  
  87.                     _context.Entry(newSetting).State = EntityState.Added;
  88.                 }
  89.  
  90.             }
  91.             _context.SaveChanges();
  92.             await Task.CompletedTask;
  93.         }
  94.  
  95.  
  96.         public IEnumerable<AllocationSetting> GetSettings()
  97.         {  
  98.             var allocationSettings = _context.AllocationSettings
  99.                 .Where(e => e.EmployeeId == loggedUserId)
  100.                 .Select(x => new AllocationSetting()
  101.                 {
  102.                     Id = x.Id,
  103.                     InternalName = x.InternalName,
  104.                     Value = x.Value
  105.                 }).ToList();
  106.                
  107.  
  108.             if(!allocationSettings.Any())
  109.             {
  110.                 allocationSettings = _context.AllocationSettings
  111.                 .Where(e => e.EmployeeId == defaultUser)
  112.                 .Select(x => new AllocationSetting()
  113.                 {
  114.                     Id = x.Id,
  115.                     InternalName = x.InternalName,
  116.                     Value = x.Value
  117.                 }).ToList();
  118.             }
  119.  
  120.             return allocationSettings;
  121.  
  122.         }
  123.         public async Task UpdateSettings(AllocationSetting settings)
  124.         {
  125.             var settingsDb = _context.AllocationSettings
  126.                 .Where(x => x.Id == settings.Id)
  127.                 .SingleOrDefault();
  128.  
  129.             if (settingsDb != null)
  130.             {
  131.                 settingsDb.InternalName = settings.InternalName;
  132.                 settingsDb.Value = (int)settings.Value;
  133.                 settingsDb.EmployeeId = (int)settings.Employee.Id;
  134.                 _context.Entry(settingsDb).State = EntityState.Modified;
  135.                 _context.SaveChanges();
  136.             }
  137.             await Task.CompletedTask;
  138.         }
  139.  
  140.  
  141.         public IEnumerable<AttributeColumn> GetAttributeColumns()
  142.         {
  143.             var attributeColumns = _context.AttributeColumnsSettings.ToList();
  144.             return attributeColumns
  145.                 .Select(x => new AttributeColumn()
  146.                 {
  147.                     Id = x.Id,
  148.                     AttributeClassId = x.AttributeId,
  149.                     Name = x.Name,
  150.                     Order = x.ColumnOrder,
  151.                     IsVisible = x.IsVisible
  152.                 });
  153.         }
  154.  
  155.         public async Task UpdateAttributeColumns(List<AttributeColumn> attributeColumnsList)
  156.         {
  157.             foreach(var el in attributeColumnsList)
  158.             {
  159.                 var attributeColDb = _context.AttributeColumnsSettings
  160.                     .Where(x => x.Id == el.Id)
  161.                     .FirstOrDefault();
  162.                 if(attributeColDb != null)
  163.                 {
  164.                     attributeColDb.AttributeId = (int)el.AttributeClassId;
  165.                     attributeColDb.ColumnOrder = (int)el.Order;
  166.                     attributeColDb.IsVisible = (bool)el.IsVisible;
  167.                     attributeColDb.EmployeeId = (int)el.Employee.Id;
  168.                     _context.Entry(attributeColDb).State = EntityState.Modified;
  169.                 }
  170.                 _context.SaveChanges();
  171.                 await Task.CompletedTask;
  172.             }
  173.         }
  174.  
  175.         public IEnumerable<ColumnImportanceSetting> GetColumnImportanceSettings()
  176.         {
  177.             var importanceColumnSetting = _context.ColumnsImportanceSettings
  178.                 .Select(s => new ColumnImportanceSetting()
  179.                 {
  180.                     Id = s.Id,
  181.                     InternalName = s.InternalName,
  182.                     Value = s.Value,
  183.                     IsSelected = s.IsSelected
  184.                 })
  185.                 .ToList();
  186.  
  187.             return importanceColumnSetting;
  188.         }
  189.  
  190.         public Task UpdateColumnImportanceSettings(List<ColumnImportanceSetting> columnSettings)
  191.         {
  192.             throw new NotImplementedException();
  193.         }
  194.     }
  195. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement