public async Task <ProfileDto> EditProfileByIdAsync(string ownerId, ProfileReq input)
        {
            try
            {
                var existingProfile = await _dbContext.ProfileModels.Include(x => x.Competences)
                                      .Include(x => x.ImageModel)
                                      .Include(x => x.ProjectProfileRoles).ThenInclude(x => x.ProjectModel).ThenInclude(x => x.TimePeriod)
                                      .Include(x => x.ProjectProfileRoles).ThenInclude(x => x.ProjectModel).ThenInclude(x => x.Techniques)
                                      .FirstOrDefaultAsync(x => x.OwnerID == ownerId);

                if (input.OfficeModelId != 0)
                {
                    _dbContext.Entry(existingProfile).Property("OfficeModelId").CurrentValue = input.OfficeModelId;
                }

                existingProfile.AboutMe     = input.AboutMe;
                existingProfile.FirstName   = input.FirstName;
                existingProfile.LastName    = input.LastName;
                existingProfile.Position    = input.Position;
                existingProfile.LinkedInUrl = input.LinkedInUrl;
                existingProfile.ResumeUrl   = input.ResumeUrl;
                existingProfile.Modified    = DateTime.UtcNow;
                foreach (var competence in input.Competences)
                {
                    var existingCompetence = existingProfile.Competences.FirstOrDefault(c => c.Id == competence.Id);
                    if (existingCompetence == null)
                    {
                        existingProfile.Competences.Add(competence);
                    }
                    else
                    {
                        _dbContext.Entry(existingCompetence).CurrentValues.SetValues(competence);
                    }
                }

                foreach (var competence in existingProfile.Competences)
                {
                    if (!input.Competences.Any(c => c.Id == competence.Id))
                    {
                        _dbContext.Remove(competence);
                    }
                }
                _dbContext.Update(existingProfile);
                await _dbContext.SaveChangesAsync();

                return(_mapper.Map <ProfileModel, ProfileDto>(existingProfile));
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }
        public async Task Seed()
        {
            await _context.Database.EnsureCreatedAsync();


            if (_context.Users.Any())
            {
                foreach (var u in _context.Users)
                {
                    _context.Remove(u);
                }
                _context.SaveChanges();
            }

            var email = "*****@*****.**";

            if (await _userManager.FindByEmailAsync(email) == null)
            {
                // use the create rather than addorupdate so can set password
                var user = new ApplicationUser
                {
                    Email    = email,
                    UserName = email
                };
                await _userManager.CreateAsync(user, "test123456");
            }

            var createdUser = await _userManager.FindByEmailAsync(email);

            var roleName = "admin";

            if (await _roleManager.FindByNameAsync(roleName) == null)
            {
                await _roleManager.CreateAsync(new IdentityRole()
                {
                    Name = roleName
                });
            }

            if (!await _userManager.IsInRoleAsync(createdUser, roleName))
            {
                await _userManager.AddToRoleAsync(createdUser, roleName);
            }
            await _context.SaveChangesAsync();
        }
示例#3
0
            public async Task <Unit> Handle(Command request, CancellationToken cancellationToken)
            {
                var activity = await _context.Activities.FindAsync(request.Id);

                if (activity == null)
                {
                    throw new RestException(HttpStatusCode.NotFound, new { activity = "Not found" });
                }

                _context.Remove(activity);

                var success = await _context.SaveChangesAsync() > 0;

                if (success)
                {
                    return(Unit.Value);
                }

                throw new Exception("Problem saving changes");
            }
示例#4
0
            public async Task <Unit> Handle(Command request, CancellationToken cancellationToken)
            {
                var activity = await _dbContext.Activities.FindAsync(request.Id);

                if (activity == null)
                {
                    throw new Exception("Could not find activity");
                }

                _dbContext.Remove(activity);

                var success = await _dbContext.SaveChangesAsync() > 0;

                if (success)
                {
                    return(Unit.Value);
                }

                throw new Exception("Problem saving changes");
            }
示例#5
0
 public void Delete <T>(T entity) where T : class
 {
     _context.Remove(entity);
 }