using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using AutoMapper; using Flexitime.DataAccess.EF; using Flexitime.DataAccess.Objects; using Flexitime.Interfaces; using Flexitime.Objects; using Microsoft.EntityFrameworkCore; using LogSourceDescriptor = Flexitime.Objects.LogSourceDescriptor; namespace Flexitime.DataAccess { public class Database : IDataAccess { private FlexitimeDbContext db; private IMapper mapper; public Database(IConnectionStringProvider connectionStringProvider) { db = new FlexitimeDbContext(connectionStringProvider); db.Database.EnsureCreated(); db.InsertBaseData(); mapper = MappingHelper.CreateMapper(new UserProfile(), new TimeLogProfile(), new GroupProfile(), new TeamProfile(), new PermissionsProfile(), new ApplicationProfile(), new IdentifierProfile(), new LogDescriptorProfile()); } public Task> GetUsers() { return Task.Run(() => mapper.Map>(db.Users .Include(x => x.AssociatedIdentifiers) .Include(x => x.DirectReports) //.Include(x=>x.Groups) //.Include(x=>x.Permissions) .Include(x => x.Team))); } public Task GetUserById(Guid id) { return Task.Run(()=>mapper.Map( db.Users.Where(x => x.Id == id) .Include(x => x.AssociatedIdentifiers) .Include(x => x.DirectReports) //.Include(x=>x.Groups) .Include(x => x.Permissions) .Include(x => x.Team) .FirstOrDefault())); } public async Task AddUser(User newUser) { if (newUser.Id != Guid.Empty) { var existingUser = await GetUserById(newUser.Id); if (existingUser != null) { return null; } } newUser.Id = Guid.Empty; var userToAdd = mapper.Map(newUser); userToAdd.Id = Guid.Empty; var result = db.Users.Add(userToAdd); await db.SaveChangesAsync(); return mapper.Map(result.Entity); } public Task GetUserByUsername(string userName) { return Task.Run(() => mapper.Map( db.Users.Where(x => x.UserName == userName) .Include(x => x.AssociatedIdentifiers) .Include(x => x.DirectReports) //.Include(x=>x.Groups) .Include(x => x.Permissions) .Include(x => x.Team) .FirstOrDefault())); } public async Task UpdateUser(User user) { var userToUpdate = mapper.Map(user); var existing = db.Users.FirstOrDefault(x => x.Id == user.Id); existing.AssociatedIdentifiers = userToUpdate.AssociatedIdentifiers; existing.LineManager = userToUpdate.LineManager; existing.Permissions = userToUpdate.Permissions; existing.UserName = userToUpdate.UserName; existing.DirectReports = userToUpdate.DirectReports; existing.FirstName = userToUpdate.FirstName; existing.LastName = userToUpdate.LastName; existing.IsContractor = userToUpdate.IsContractor; existing.Groups = userToUpdate.Groups; existing.HoursPerWeek = userToUpdate.HoursPerWeek; existing.Password = userToUpdate.Password; existing.Team = userToUpdate.Team; existing.TeamId = userToUpdate.Team.Id; db.Users.Update(existing); await db.SaveChangesAsync(); return await GetUserById(user.Id); } } internal static class MappingHelper { public static IMapper CreateMapper(params Profile[] profiles) { var _profiles = profiles.ToList(); var mapperCfg = new MapperConfiguration(x => _profiles.ForEach(x.AddProfile)); mapperCfg.AssertConfigurationIsValid(); return new Mapper(mapperCfg); } } public class UserProfile : Profile { public UserProfile() { CreateMap() .ForMember(x => x.State, opt => opt.MapFrom(x => (int)x.State)) .ReverseMap(); CreateMap() .ForMember(dst => dst.State, opt => opt.MapFrom(x => Enum.Parse(typeof(UserState), x.State.ToString()))) .ForMember(dst => dst.DirectReportIds, opt => opt.MapFrom(src => src.DirectReports.Select(x => x.Id))) .ReverseMap(); } } public class TimeLogProfile : Profile { public TimeLogProfile() { CreateMap() .ReverseMap(); CreateMap() .ReverseMap(); } } public class GroupProfile : Profile { public GroupProfile() { CreateMap() .ReverseMap(); CreateMap() .ForMember(dst => dst.UserCount, opt => opt.Ignore()) .ReverseMap(); } } public class TeamProfile : Profile { public TeamProfile() { CreateMap() .ReverseMap(); CreateMap() .ForMember(dst => dst.Users, opt => opt.MapFrom(src => src.Members.Users)) .ReverseMap(); } } public class PermissionsProfile : Profile { public PermissionsProfile() { CreateMap() .ReverseMap(); CreateMap() .ReverseMap(); } } public class ApplicationProfile : Profile { public ApplicationProfile() { CreateMap() .ReverseMap(); CreateMap() .ReverseMap(); } } public class IdentifierProfile : Profile { public IdentifierProfile() { CreateMap() .ReverseMap(); CreateMap() .ReverseMap(); } } public class LogDescriptorProfile : Profile { public LogDescriptorProfile() { CreateMap() .ReverseMap(); CreateMap() .ReverseMap(); } } }