Skip to content

Commit

Permalink
feat: update remove or update users
Browse files Browse the repository at this point in the history
  • Loading branch information
Qinyouzeng committed Jan 13, 2025
1 parent ce06c0d commit 1aebac2
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,21 +72,8 @@ await _appRepository.IsExistedApp(
}

appEntity.Name = appModel.Name;
appEntity.Description = appModel.Description;

if (appEntity.ResponsibilityUsers != null)
{
appEntity.ResponsibilityUsers.RemoveAll(r => !appModel.ResponsibilityUsers.Contains(r.UserId));
appEntity.ResponsibilityUsers.AddRange(appModel.ResponsibilityUsers
.Where(userId => !appEntity.ResponsibilityUsers.Select(r => r.UserId).Contains(userId))
.Select(userId => new AppResponsibilityUser { UserId = userId, AppId = appEntity.Id }));
}
else
{
appEntity.ResponsibilityUsers = appModel.ResponsibilityUsers.Select(userId => new AppResponsibilityUser { UserId = userId, AppId = appEntity.Id }).ToList();
}

await _appRepository.UpdateAsync(appEntity);
appEntity.Description = appModel.Description;
await _appRepository.UpdateAsync(appEntity,appModel.ResponsibilityUsers);

var envClusterProjectApps = await _appRepository.GetEnvironmentClusterProjectAppsByAppId(appModel.Id);
await _appRepository.RemoveEnvironmentClusterProjectApps(envClusterProjectApps);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public interface IAppRepository

Task<List<(int ProjectId, App App, EnvironmentClusterProjectApp EnvironmentClusterProjectApp)>> GetAppByEnvNameAndProjectIdsAsync(string envName, IEnumerable<int> projectIds);

Task UpdateAsync(App app);
Task UpdateAsync(App app,List<Guid> users);

Task RemoveAsync(int Id);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the Apache License. See LICENSE.txt in the project root for license information.

using MASA.PM.Contracts.Admin.Model;

namespace MASA.PM.Infrastructure.Repository.Repositories;

internal class AppRepository : IAppRepository
Expand Down Expand Up @@ -150,10 +152,29 @@ join app in _dbContext.Apps.Include(x => x.ResponsibilityUsers) on environmentCl
return result;
}

public async Task UpdateAsync(App app)
public async Task UpdateAsync(App app, List<Guid> userIds)
{
_dbContext.Apps.Update(app);

if (app.ResponsibilityUsers != null)
{
if (userIds == null || !userIds.Any())
app.ResponsibilityUsers.Clear();
else
{
app.ResponsibilityUsers.RemoveAll(r => !userIds.Contains(r.UserId));
foreach (var userId in userIds)
{
if (!app.ResponsibilityUsers.Any(r => r.UserId == userId))
{
app.ResponsibilityUsers.Add(new AppResponsibilityUser { UserId = userId, AppId = app.Id });
}
}
}
}
else if (userIds != null && userIds.Any())
{
app.ResponsibilityUsers = userIds.Select(userId => new AppResponsibilityUser { UserId = userId, AppId = app.Id }).ToList();
}
await _dbContext.SaveChangesAsync();
}

Expand Down

0 comments on commit 1aebac2

Please sign in to comment.