Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Threading.Tasks;
- using Crud.AllocationConfigurations;
- using Crud.AllocationConfigurations.AttributeColumnsSettings;
- using Crud.AllocationConfigurations.Models;
- using Microsoft.EntityFrameworkCore;
- using Web.Api.Core.Crud.Employees;
- using Web.Api.Infrastructure.EF.SqlServer.Context;
- using WebApi;
- namespace EF.SqlServer.Repositories
- {
- public class AllocationConfigurationRepository : IAllocationConfigurationRepository
- {
- private readonly GiacomoContiContext _context;
- private readonly int loggedUserId = 7; //TODO: Fixed when user login finished
- private readonly int defaultUser = 12; // From where?
- public AllocationConfigurationRepository(GiacomoContiContext context)
- {
- _context = context;
- }
- public IEnumerable<StoresColumnSetting> GetStoreColumnsSettings()
- {
- var visibleAndOrderSettings = _context.StoreColumnsSettings
- .Where(u => u.EmployeeId == loggedUserId)
- .Select(x => new StoresColumnSetting()
- {
- Id = x.Id,
- InternalName = x.InternalName,
- Order = x.ColumnOrder,
- IsVisible = x.IsVisible
- })
- .ToList();
- if (!visibleAndOrderSettings.Any())
- {
- visibleAndOrderSettings = _context.StoreColumnsSettings
- .Where(e => e.EmployeeId == defaultUser)
- .Select(x => new StoresColumnSetting()
- {
- Id = x.Id,
- InternalName = x.InternalName,
- Order = x.ColumnOrder,
- IsVisible = x.IsVisible
- })
- .ToList();
- }
- return visibleAndOrderSettings;
- }
- public async Task UpdateStoreColumnsSettings(List<StoresColumnSetting> storesColumnsSettingsList)
- {
- var employeeSettings = _context.StoreColumnsSettings
- .Where(e => e.EmployeeId == loggedUserId)
- .ToList();
- if (employeeSettings.Any())
- {
- foreach (var item in storesColumnsSettingsList)
- {
- var dbExistingSetting = _context.StoreColumnsSettings
- .Where(s => s.Id == item.Id)
- .FirstOrDefault();
- if (dbExistingSetting != null)
- {
- dbExistingSetting.ColumnOrder = item.Order;
- dbExistingSetting.IsVisible = item.IsVisible;
- }
- _context.Entry(dbExistingSetting).State = EntityState.Modified;
- }
- }
- else
- {
- foreach (var item in storesColumnsSettingsList)
- {
- var newSetting = new StoreColumnsSettings()
- {
- InternalName = item.InternalName,
- ColumnOrder = item.Order,
- IsVisible = item.IsVisible,
- EmployeeId = loggedUserId
- };
- _context.StoreColumnsSettings.Add(newSetting);
- _context.Entry(newSetting).State = EntityState.Added;
- }
- }
- _context.SaveChanges();
- await Task.CompletedTask;
- }
- public IEnumerable<AllocationSetting> GetSettings()
- {
- var allocationSettings = _context.AllocationSettings
- .Where(e => e.EmployeeId == loggedUserId)
- .Select(x => new AllocationSetting()
- {
- Id = x.Id,
- InternalName = x.InternalName,
- Value = x.Value
- }).ToList();
- if(!allocationSettings.Any())
- {
- allocationSettings = _context.AllocationSettings
- .Where(e => e.EmployeeId == defaultUser)
- .Select(x => new AllocationSetting()
- {
- Id = x.Id,
- InternalName = x.InternalName,
- Value = x.Value
- }).ToList();
- }
- return allocationSettings;
- }
- public async Task UpdateSettings(AllocationSetting settings)
- {
- var settingsDb = _context.AllocationSettings
- .Where(x => x.Id == settings.Id)
- .SingleOrDefault();
- if (settingsDb != null)
- {
- settingsDb.InternalName = settings.InternalName;
- settingsDb.Value = (int)settings.Value;
- settingsDb.EmployeeId = (int)settings.Employee.Id;
- _context.Entry(settingsDb).State = EntityState.Modified;
- _context.SaveChanges();
- }
- await Task.CompletedTask;
- }
- public IEnumerable<AttributeColumn> GetAttributeColumns()
- {
- var attributeColumns = _context.AttributeColumnsSettings.ToList();
- return attributeColumns
- .Select(x => new AttributeColumn()
- {
- Id = x.Id,
- AttributeClassId = x.AttributeId,
- Name = x.Name,
- Order = x.ColumnOrder,
- IsVisible = x.IsVisible
- });
- }
- public async Task UpdateAttributeColumns(List<AttributeColumn> attributeColumnsList)
- {
- foreach(var el in attributeColumnsList)
- {
- var attributeColDb = _context.AttributeColumnsSettings
- .Where(x => x.Id == el.Id)
- .FirstOrDefault();
- if(attributeColDb != null)
- {
- attributeColDb.AttributeId = (int)el.AttributeClassId;
- attributeColDb.ColumnOrder = (int)el.Order;
- attributeColDb.IsVisible = (bool)el.IsVisible;
- attributeColDb.EmployeeId = (int)el.Employee.Id;
- _context.Entry(attributeColDb).State = EntityState.Modified;
- }
- _context.SaveChanges();
- await Task.CompletedTask;
- }
- }
- public IEnumerable<ColumnImportanceSetting> GetColumnImportanceSettings()
- {
- var importanceColumnSetting = _context.ColumnsImportanceSettings
- .Select(s => new ColumnImportanceSetting()
- {
- Id = s.Id,
- InternalName = s.InternalName,
- Value = s.Value,
- IsSelected = s.IsSelected
- })
- .ToList();
- return importanceColumnSetting;
- }
- public Task UpdateColumnImportanceSettings(List<ColumnImportanceSetting> columnSettings)
- {
- throw new NotImplementedException();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement