reverts MemberGroupController and removes TODOs
This commit is contained in:
@@ -1,9 +1,7 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Identity;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Umbraco.Cms.Core;
|
||||
using Umbraco.Cms.Core.Mapping;
|
||||
@@ -28,44 +26,30 @@ namespace Umbraco.Cms.Web.BackOffice.Controllers
|
||||
private readonly IMemberGroupService _memberGroupService;
|
||||
private readonly UmbracoMapper _umbracoMapper;
|
||||
private readonly ILocalizedTextService _localizedTextService;
|
||||
private readonly RoleManager<IdentityRole> _roleManager;
|
||||
|
||||
public MemberGroupController(
|
||||
IMemberGroupService memberGroupService,
|
||||
UmbracoMapper umbracoMapper,
|
||||
ILocalizedTextService localizedTextService,
|
||||
RoleManager<IdentityRole> roleManager
|
||||
)
|
||||
ILocalizedTextService localizedTextService)
|
||||
{
|
||||
_memberGroupService = memberGroupService ?? throw new ArgumentNullException(nameof(memberGroupService));
|
||||
_roleManager = roleManager ?? throw new ArgumentNullException(nameof(roleManager));
|
||||
_umbracoMapper = umbracoMapper ?? throw new ArgumentNullException(nameof(umbracoMapper));
|
||||
_localizedTextService = localizedTextService ?? throw new ArgumentNullException(nameof(localizedTextService));
|
||||
}
|
||||
|
||||
//TODO: are there any repercussions elsewhere for us changing these to async?
|
||||
|
||||
/// <summary>
|
||||
/// Gets the member group json for the member group id
|
||||
/// </summary>
|
||||
/// <param name="id"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<ActionResult<MemberGroupDisplay>> GetById(int id)
|
||||
public ActionResult<MemberGroupDisplay> GetById(int id)
|
||||
{
|
||||
//TODO: did we envisage this - combination of service and identity manager?
|
||||
IdentityRole identityRole = await _roleManager.FindByIdAsync(id.ToString());
|
||||
if (identityRole == null)
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
|
||||
IMemberGroup memberGroup = _memberGroupService.GetById(id);
|
||||
if (memberGroup == null)
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
|
||||
//TODO: the default identity role doesn't have all the properties IMemberGroup had, e.g. CreatorId
|
||||
MemberGroupDisplay dto = _umbracoMapper.Map<IMemberGroup, MemberGroupDisplay>(memberGroup);
|
||||
return dto;
|
||||
}
|
||||
@@ -75,14 +59,8 @@ namespace Umbraco.Cms.Web.BackOffice.Controllers
|
||||
/// </summary>
|
||||
/// <param name="id"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<ActionResult<MemberGroupDisplay>> GetById(Guid id)
|
||||
public ActionResult<MemberGroupDisplay> GetById(Guid id)
|
||||
{
|
||||
//TODO: did we envisage just identity or a combination of service and identity manager?
|
||||
//IdentityRole identityRole = await _roleManager.FindByIdAsync(id.ToString());
|
||||
//if (identityRole == null)
|
||||
//{
|
||||
// return NotFound();
|
||||
//}
|
||||
IMemberGroup memberGroup = _memberGroupService.GetById(id);
|
||||
if (memberGroup == null)
|
||||
{
|
||||
@@ -97,7 +75,7 @@ namespace Umbraco.Cms.Web.BackOffice.Controllers
|
||||
/// </summary>
|
||||
/// <param name="id"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<ActionResult<MemberGroupDisplay>> GetById(Udi id)
|
||||
public ActionResult<MemberGroupDisplay> GetById(Udi id)
|
||||
{
|
||||
var guidUdi = id as GuidUdi;
|
||||
if (guidUdi == null)
|
||||
@@ -105,13 +83,6 @@ namespace Umbraco.Cms.Web.BackOffice.Controllers
|
||||
return NotFound();
|
||||
}
|
||||
|
||||
//TODO: can we do this via identity?
|
||||
IdentityRole identityRole = await _roleManager.FindByIdAsync(id.ToString());
|
||||
if (identityRole == null)
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
|
||||
IMemberGroup memberGroup = _memberGroupService.GetById(guidUdi.Guid);
|
||||
if (memberGroup == null)
|
||||
{
|
||||
@@ -122,47 +93,25 @@ namespace Umbraco.Cms.Web.BackOffice.Controllers
|
||||
}
|
||||
|
||||
public IEnumerable<MemberGroupDisplay> GetByIds([FromQuery] int[] ids)
|
||||
{
|
||||
//var roles = new List<IdentityRole>();
|
||||
|
||||
//foreach (int id in ids)
|
||||
//{
|
||||
// IdentityRole role = await _roleManager.FindByIdAsync(id.ToString());
|
||||
// roles.Add(role);
|
||||
//}
|
||||
|
||||
//return roles.Select(x => _umbracoMapper.Map<IdentityRole, MemberGroupDisplay>(x));
|
||||
//TODO: does this need to be done via identity?
|
||||
|
||||
return _memberGroupService.GetByIds(ids)
|
||||
.Select(_umbracoMapper.Map<IMemberGroup, MemberGroupDisplay>);
|
||||
}
|
||||
=> _memberGroupService.GetByIds(ids).Select(_umbracoMapper.Map<IMemberGroup, MemberGroupDisplay>);
|
||||
|
||||
[HttpDelete]
|
||||
[HttpPost]
|
||||
public async Task<IActionResult> DeleteById(int id)
|
||||
public IActionResult DeleteById(int id)
|
||||
{
|
||||
IdentityRole role = await _roleManager.FindByIdAsync(id.ToString());
|
||||
|
||||
if (role == null)
|
||||
var memberGroup = _memberGroupService.GetById(id);
|
||||
if (memberGroup == null)
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
|
||||
IdentityResult roleDeleted = await _roleManager.DeleteAsync(role);
|
||||
if (roleDeleted.Succeeded)
|
||||
{
|
||||
return Ok();
|
||||
}
|
||||
else
|
||||
{
|
||||
return Problem("Issue during deletion - please see logs");
|
||||
}
|
||||
_memberGroupService.Delete(memberGroup);
|
||||
return Ok();
|
||||
}
|
||||
|
||||
//TODO: we don't currently implement IQueryableRoleStore<TRole>, still using original service
|
||||
//public IEnumerable<MemberGroupDisplay> GetAllGroups() => _roleManager.Roles.Select(x => _umbracoMapper.Map<IdentityRole, MemberGroupDisplay>(x));
|
||||
public IEnumerable<MemberGroupDisplay> GetAllGroups() => _memberGroupService.GetAll().Select(x => _umbracoMapper.Map<IMemberGroup, MemberGroupDisplay>(x));
|
||||
public IEnumerable<MemberGroupDisplay> GetAllGroups()
|
||||
=> _memberGroupService.GetAll()
|
||||
.Select(_umbracoMapper.Map<IMemberGroup, MemberGroupDisplay>);
|
||||
|
||||
public MemberGroupDisplay GetEmpty()
|
||||
{
|
||||
@@ -170,47 +119,19 @@ namespace Umbraco.Cms.Web.BackOffice.Controllers
|
||||
return _umbracoMapper.Map<IMemberGroup, MemberGroupDisplay>(item);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Saves the member group via the identity role
|
||||
/// If new, creates a new role, else updates the existing role
|
||||
/// </summary>
|
||||
/// <param name="saveModel"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<ActionResult<MemberGroupDisplay>> PostSave(MemberGroupSave saveModel)
|
||||
public ActionResult<MemberGroupDisplay> PostSave(MemberGroupSave saveModel)
|
||||
{
|
||||
int id = int.Parse(saveModel.Id.ToString());
|
||||
|
||||
IdentityRole role;
|
||||
IdentityResult updatedResult;
|
||||
if (id > 0)
|
||||
{
|
||||
role = await _roleManager.FindByIdAsync(saveModel.Id.ToString());
|
||||
role.Name = saveModel.Name;
|
||||
updatedResult = await _roleManager.UpdateAsync(role);
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
role = new IdentityRole(saveModel.Name);
|
||||
updatedResult = await _roleManager.CreateAsync(role);
|
||||
}
|
||||
|
||||
if (!updatedResult.Succeeded)
|
||||
{
|
||||
//TODO: what to return if there is a failed identity result
|
||||
return Problem();
|
||||
}
|
||||
|
||||
//TODO: do we need to refetch the member group?
|
||||
int roleId = int.Parse(role.Id);
|
||||
IMemberGroup memberGroup = _memberGroupService.GetById(roleId);
|
||||
|
||||
var id = int.Parse(saveModel.Id.ToString());
|
||||
IMemberGroup memberGroup = id > 0 ? _memberGroupService.GetById(id) : new MemberGroup();
|
||||
if (memberGroup == null)
|
||||
{
|
||||
return NotFound();
|
||||
}
|
||||
|
||||
//TODO: should we return the identity role or return the group from the service?
|
||||
memberGroup.Name = saveModel.Name;
|
||||
_memberGroupService.Save(memberGroup);
|
||||
|
||||
MemberGroupDisplay display = _umbracoMapper.Map<IMemberGroup, MemberGroupDisplay>(memberGroup);
|
||||
|
||||
display.AddSuccessNotification(
|
||||
|
||||
Reference in New Issue
Block a user