229 lines
7.1 KiB
C#
229 lines
7.1 KiB
C#
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<List<User>> GetUsers()
|
|
{
|
|
return Task.Run(() => mapper.Map<List<User>>(db.Users
|
|
.Include(x => x.AssociatedIdentifiers)
|
|
.Include(x => x.DirectReports)
|
|
//.Include(x=>x.Groups)
|
|
//.Include(x=>x.Permissions)
|
|
.Include(x => x.Team)));
|
|
}
|
|
|
|
public Task<User> GetUserById(Guid id)
|
|
{
|
|
return Task.Run(()=>mapper.Map<User>(
|
|
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<User> 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<UserDb>(newUser);
|
|
userToAdd.Id = Guid.Empty;
|
|
|
|
var result = db.Users.Add(userToAdd);
|
|
|
|
await db.SaveChangesAsync();
|
|
|
|
return mapper.Map<User>(result.Entity);
|
|
}
|
|
|
|
public Task<User> GetUserByUsername(string userName)
|
|
{
|
|
return Task.Run(() => mapper.Map<User>(
|
|
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<User> UpdateUser(User user)
|
|
{
|
|
var userToUpdate = mapper.Map<UserDb>(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<User, UserDb>()
|
|
.ForMember(x => x.State,
|
|
opt => opt.MapFrom(x => (int)x.State))
|
|
.ReverseMap();
|
|
|
|
CreateMap<UserDb, User>()
|
|
.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<TimeLog, TimeLogDb>()
|
|
.ReverseMap();
|
|
|
|
CreateMap<TimeLogDb, TimeLog>()
|
|
.ReverseMap();
|
|
}
|
|
}
|
|
|
|
public class GroupProfile : Profile
|
|
{
|
|
public GroupProfile()
|
|
{
|
|
CreateMap<Group, GroupDb>()
|
|
.ReverseMap();
|
|
CreateMap<GroupDb, Group>()
|
|
.ForMember(dst => dst.UserCount, opt => opt.Ignore())
|
|
.ReverseMap();
|
|
}
|
|
}
|
|
|
|
public class TeamProfile : Profile
|
|
{
|
|
public TeamProfile()
|
|
{
|
|
CreateMap<Team, TeamDb>()
|
|
.ReverseMap();
|
|
CreateMap<TeamDb, Team>()
|
|
.ForMember(dst => dst.Users, opt => opt.MapFrom(src => src.Members.Users))
|
|
.ReverseMap();
|
|
}
|
|
}
|
|
|
|
public class PermissionsProfile : Profile
|
|
{
|
|
public PermissionsProfile()
|
|
{
|
|
CreateMap<Permission, PermissionDb>()
|
|
.ReverseMap();
|
|
CreateMap<PermissionDb, Permission>()
|
|
.ReverseMap();
|
|
}
|
|
}
|
|
|
|
public class ApplicationProfile : Profile
|
|
{
|
|
public ApplicationProfile()
|
|
{
|
|
CreateMap<Application, ApplicationDb>()
|
|
.ReverseMap();
|
|
CreateMap<ApplicationDb, Application>()
|
|
.ReverseMap();
|
|
}
|
|
}
|
|
|
|
public class IdentifierProfile : Profile
|
|
{
|
|
public IdentifierProfile()
|
|
{
|
|
CreateMap<Identifier, IdentifierDb>()
|
|
.ReverseMap();
|
|
CreateMap<IdentifierDb, Identifier>()
|
|
.ReverseMap();
|
|
}
|
|
}
|
|
|
|
public class LogDescriptorProfile : Profile
|
|
{
|
|
public LogDescriptorProfile()
|
|
{
|
|
CreateMap<LogSourceDescriptor, LogSourceDescriptorDb>()
|
|
.ReverseMap();
|
|
CreateMap<LogSourceDescriptorDb, LogSourceDescriptor>()
|
|
.ReverseMap();
|
|
}
|
|
}
|
|
}
|