Skip to content

Commit

Permalink
update account component 6.22
Browse files Browse the repository at this point in the history
  • Loading branch information
dong-nguyen-hd committed Jun 22, 2022
1 parent 6785b60 commit b7f19ac
Show file tree
Hide file tree
Showing 6 changed files with 138 additions and 46 deletions.
16 changes: 16 additions & 0 deletions BE/API/Controllers/GroupController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,22 @@ public async Task<IActionResult> AddGroupToAccountAsync([FromBody] GroupIdResour
return StatusCode(201, result);
}

[HttpPost("remove-account")]
[Authorize(Roles = $"{Role.EditorQTDA}")]
[ProducesResponseType(typeof(BaseResponse<GroupResource>), StatusCodes.Status200OK)]
[ProducesResponseType(typeof(BaseResponse<GroupResource>), StatusCodes.Status400BadRequest)]
public async Task<IActionResult> RemoveGroupFromAccountAsync([FromBody] GroupIdResource resource)
{
Log.Information($"{User.Identity?.Name}: remove group-id '{resource.GroupId}' from account-id '{resource.AccountId}'.");

var result = await _groupService.RemoveGroupFromAccountAsync(resource.AccountId, resource.GroupId);

if (!result.Success)
return BadRequest(result);

return Ok(result);
}

[HttpPut("{id:int}")]
[Authorize(Roles = $"{Role.Admin}, {Role.EditorQTNS}, {Role.EditorQTDA}, {Role.Viewer}")]
[ProducesResponseType(typeof(BaseResponse<GroupResource>), StatusCodes.Status200OK)]
Expand Down
2 changes: 1 addition & 1 deletion BE/Business/Domain/Repositories/IAccountRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@ public interface IAccountRepository : IBaseRepository<Account>
Task<bool> ValidateUserNameAsync(string userName);
Task<Account> ValidateCredentialsAsync(LoginResource loginResource);
Task<Account> GetByIdAsync(int id, bool hasToken);
Task<Account> GetByIdIncludeGroupAsync(int accountId);
Task<Account> GetByIdIncludeGroupAsync(int accountId, bool isTracking = false);
}
}
1 change: 1 addition & 0 deletions BE/Business/Domain/Services/IGroupService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ public interface IGroupService : IBaseService<GroupResource, CreateGroupResource
{
Task<PaginationResponse<IEnumerable<GroupResource>>> GetPaginationAsync(QueryResource pagination, FilterGroupResource filterResource);
Task<BaseResponse<GroupResource>> AddGroupToAccountAsync(int accountId, int groupId);
Task<BaseResponse<GroupResource>> RemoveGroupFromAccountAsync(int accountId, int groupId);
}
}
28 changes: 27 additions & 1 deletion BE/Business/Services/GroupService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,33 @@ public async Task<BaseResponse<GroupResource>> AddGroupToAccountAsync(int accoun
if (tempGroup is null)
return new BaseResponse<GroupResource>(ResponseMessage.Values["Group_NoData"]);

tempAccount.Groups.Add(tempGroup);

tempAccount.Groups = new() { tempGroup };
await UnitOfWork.CompleteAsync();

return new BaseResponse<GroupResource>(Mapper.Map<Group, GroupResource>(tempGroup));
}
catch (Exception ex)
{
throw new MessageResultException(ResponseMessage.Values["Group_Saving_Error"], ex);
}
}

public async Task<BaseResponse<GroupResource>> RemoveGroupFromAccountAsync(int accountId, int groupId)
{
try
{
// Validate account-Id is existent?
var tempAccount = await _accountRepository.GetByIdIncludeGroupAsync(accountId, true);
if (tempAccount is null)
return new BaseResponse<GroupResource>(ResponseMessage.Values["Account_NoData"]);

// Validate group-Id is existent?
var tempGroup = await _groupRepository.GetByIdAsync(groupId);
if (tempGroup is null)
return new BaseResponse<GroupResource>(ResponseMessage.Values["Group_NoData"]);

tempAccount.Groups.Remove(tempGroup);
await UnitOfWork.CompleteAsync();

return new BaseResponse<GroupResource>(Mapper.Map<Group, GroupResource>(tempGroup));
Expand Down
14 changes: 8 additions & 6 deletions BE/Infrastructure/Repositories/AccountRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@ public AccountRepository(AppDbContext context) : base(context) { }
#endregion

#region Method
public async Task<Account> GetByIdIncludeGroupAsync(int accountId) =>
await Context.Accounts.Where(x => x.Id.Equals(accountId))
.AsNoTracking()
.AsSplitQuery()
.Include(x => x.Groups)
.SingleOrDefaultAsync();
public async Task<Account> GetByIdIncludeGroupAsync(int accountId, bool isTracking = false)
{
var queryable = Context.Accounts.Where(x => x.Id.Equals(accountId));

if (!isTracking) queryable = queryable.AsNoTracking();

return await queryable.AsSplitQuery().Include(x => x.Groups).SingleOrDefaultAsync();
}

public async Task<Account> GetByIdAsync(int id, bool hasToken)
{
Expand Down
123 changes: 85 additions & 38 deletions FE/HR-Management/src/pages/Account/ListAccountQTDA.vue
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@
<q-td :props="props">
<div style="display: inline">
<q-btn
style="width: 60px"
style="width: 70px"
dense
color="negative"
text-color="white"
Expand Down Expand Up @@ -700,6 +700,10 @@ export default defineComponent({
if (result.success) {
this.listProject = result.resource.groups;
this.listProject.forEach((row, index) => {
row.index = index + 1;
});
} else {
this.$q.notify({
type: "negative",
Expand All @@ -711,50 +715,93 @@ export default defineComponent({
}
},
async addProject() {
let isValid = await this.validateToken();
if (!isValid) this.$router.replace("/login");
let isValid = await this.validateToken();
if (!isValid) this.$router.replace("/login");
let payload ={
accountId: this.tempAddAccountId,
groupId: this.tempAddProjectId
}
let payload = {
accountId: this.tempAddAccountId,
groupId: this.tempAddProjectId,
};
// Request API
let result = await api
.post(`/api/v1/group/add-account`, payload)
.then((response) => {
return response.data;
})
.catch(function (error) {
// Checking if throw error
if (error.response) {
// Server response
return error.response.data;
} else {
// Server not working
let temp = { success: false, message: ["Server Error!"] };
return temp;
}
});
// Request API
let result = await api
.post(`/api/v1/group/add-account`, payload)
.then((response) => {
return response.data;
})
.catch(function (error) {
// Checking if throw error
if (error.response) {
// Server response
return error.response.data;
} else {
// Server not working
let temp = { success: false, message: ["Server Error!"] };
return temp;
}
});
if (result.success) {
if (this.listProject.length >= 10) this.listProject.pop();
this.listProject.unshift(result.resource);
if (result.success) {
if (this.listProject.length >= 10) this.listProject.pop();
this.listProject.unshift(result.resource);
this.listProject.forEach((row, index) => {
row.index = index + 1;
});
this.listProject.forEach((row, index) => {
row.index = index + 1;
});
this.tempAddProjectId = null;
} else {
this.$q.notify({
type: "negative",
message: result.message[0],
this.tempAddProjectId = null;
this.$q.notify({
type: "positive",
message: "Successfully added!",
});
}
} else {
this.$q.notify({
type: "negative",
message: result.message[0],
});
}
},
removeProject(id) {
console.log(id);
async removeProject(id) {
let isValid = await this.validateToken();
if (!isValid) this.$router.replace("/login");
let payload = {
accountId: this.tempAddAccountId,
groupId: id,
};
// Request API
let result = await api
.post(`/api/v1/group/remove-account`, payload)
.then((response) => {
return response.data;
})
.catch(function (error) {
// Checking if throw error
if (error.response) {
// Server response
return error.response.data;
} else {
// Server not working
let temp = { success: false, message: ["Server Error!"] };
return temp;
}
});
if (result.success) {
await this.openProject(this.tempAddAccountId);
this.$q.notify({
type: "positive",
message: "Successfully removed!",
});
} else {
this.$q.notify({
type: "negative",
message: result.message[0],
});
}
},
closeProject() {
this.tempAddProjectId = null;
Expand Down

0 comments on commit b7f19ac

Please sign in to comment.