Esempio n. 1
0
        public async Task <bool> UpdateAsync(Domains.ResourceOwner resourceOwner)
        {
            using (var transaction = await _context.Database.BeginTransactionAsync().ConfigureAwait(false))
            {
                try
                {
                    var record = await _context.ResourceOwners
                                 .Include(r => r.Claims)
                                 .FirstOrDefaultAsync(r => r.Id == resourceOwner.Id).ConfigureAwait(false);

                    if (record == null)
                    {
                        return(false);
                    }

                    record.IsBlocked      = resourceOwner.IsBlocked;
                    record.UpdateDateTime = DateTime.UtcNow;
                    record.Claims         = new List <ResourceOwnerClaim>();
                    _context.ResourceOwnerClaims.RemoveRange(record.Claims);
                    if (resourceOwner.Claims != null)
                    {
                        foreach (var claim in resourceOwner.Claims)
                        {
                            record.Claims.Add(new ResourceOwnerClaim
                            {
                                Id = Guid.NewGuid().ToString(),
                                ResourceOwnerId = record.Id,
                                ClaimCode       = claim.Type,
                                Value           = claim.Value
                            });
                        }
                    }

                    await _context.SaveChangesAsync().ConfigureAwait(false);

                    transaction.Commit();
                    return(true);
                }
                catch (Exception ex)
                {
                    _managerEventSource.Failure(ex);
                    transaction.Rollback();
                    return(false);
                }
            }
        }
Esempio n. 2
0
        public async Task <bool> InsertAsync(Domains.ResourceOwner resourceOwner)
        {
            try
            {
                var user = new ResourceOwner
                {
                    Id             = resourceOwner.Id,
                    IsBlocked      = resourceOwner.IsBlocked,
                    Claims         = new List <ResourceOwnerClaim>(),
                    CreateDateTime = DateTime.UtcNow,
                    UpdateDateTime = DateTime.UtcNow
                };

                if (resourceOwner.Claims != null)
                {
                    foreach (var claim in resourceOwner.Claims)
                    {
                        user.Claims.Add(new ResourceOwnerClaim
                        {
                            Id = Guid.NewGuid().ToString(),
                            ResourceOwnerId = user.Id,
                            ClaimCode       = claim.Type,
                            Value           = claim.Value
                        });
                    }
                }

                _context.ResourceOwners.Add(user);
                await _context.SaveChangesAsync().ConfigureAwait(false);
            }
            catch (Exception ex)
            {
                _managerEventSource.Failure(ex);
                return(false);
            }

            return(true);
        }