I posted the original question here, But I think it would be nice to have someone else's eye on this code for improving the code:
public override void Update(UserProfileDto item)
{
base.Update(item);
var memberIntersestIds = _memberInterestService.GetAllPlusDeleted();
var dbIds = memberIntersestIds.Select(p => p.InterestSubCategoryId);
var union = dbIds.ToList().Union(item.InterestIds);
if (item.InterestIds != null)
{
var guids = union as Guid[] ?? union.ToArray();
foreach (var id in guids)
if (_memberInterestService.IsSubInterestExist(id))
_memberInterestService.Delete(id, item.Id, true);
foreach (var id in guids)
{
if (!_memberInterestService.IsSubInterestExist(id))
_memberInterestService.Add(new MemberInterestDto
{
UserProfileId = item.Id,
InterestSubCategoryId = id
});
else
_memberInterestService.Delete(id, item.Id, false);
}
}
else
foreach (var memberInterest in memberIntersestIds)
_memberInterestService.Delete(memberInterest.InterestSubCategoryId, item.Id, true);
}
Update: MemberIntersetService
public class MemberInterestService : IMemberInterestService
{
public IQueryable<MemberInterest> Items { get { return _dbSet.Where(x => x.IsDeleted == false); } }
public MemberInterestService(AppDbContext dbContext) : base(dbContext)
{
_dbSet = dbContext.MemberInterests;
}
public bool IsSubInterestExist(Guid subInterestId)
{
return _dbSet.Any(p => p.InterestSubCategoryId == subInterestId);
}
public bool IsSubInterestExistInItem(Guid subInterestId)
{
return Items.Any(p => p.InterestSubCategoryId == subInterestId);
}
public void Delete(Guid interestId, Guid userProfileId, bool delete)
{
var item = _dbSet.Single(x => x.InterestSubCategoryId == interestId && x.UserProfileId == userProfileId);
//soft delete
item.IsDeleted = delete;
_dbSet.Update(item);
}
public IList<MemberInterestDto> GetAllPlusDeleted()
{
return _dbSet.Select(x => Mapper.Map<MemberInterest, MemberInterestDto>(x))
.ToList();
}
}
MemberInterest
@denis – Sirwan Afifi 18 hours agobool IsSubInterestExist(Guid subInterestId); bool IsSubInterestExistInItem(Guid subInterestId); void Delete(Guid interestId, Guid userProfileId, bool delete); IList<MemberInterestDto> GetAllPlusDeleted();
– Sirwan Afifi 18 hours agoIEnumerable<Guid>
(item.InterestId) – Sirwan Afifi 17 hours ago