Ejemplo n.º 1
0
        public async Task <Avatar> Create(int userId, Avatar avatar)
        {
            using (var context = new TradePMREntities())
            {
                var user = await context.Users.FindAsync(userId);

                if (user == null)
                {
                    throw new ApplicationException($"User with id {userId} not found");
                }
                if (user.AvatarId != null)
                {
                    throw new ApplicationException("User already has an avatar");
                }
                avatar.Users.Add(user);
                var avatarCreated = context.Avatars.Add(avatar);
                var created       = await context.SaveChangesAsync();

                if (created == 0)
                {
                    return(null);
                }
                return(avatarCreated);
            }
        }
Ejemplo n.º 2
0
        public async Task <User> Create(User user)
        {
            using (var context = new TradePMREntities())
            {
                var userCreated = context.Users.Add(user);
                var created     = await context.SaveChangesAsync();

                if (created == 0)
                {
                    return(null);
                }
                return(userCreated);
            }
        }
Ejemplo n.º 3
0
        public async Task <User> Update(User user)
        {
            using (var context = new TradePMREntities())
            {
                var result = await context.Users.FindAsync(user.Id);

                if (result == null)
                {
                    return(null);
                }
                context.Entry(result).CurrentValues.SetValues(user);
                context.Entry(result).State = EntityState.Modified;
                var updated = await context.SaveChangesAsync();

                if (updated == 0)
                {
                    return(null);
                }
                return(user);
            }
        }
Ejemplo n.º 4
0
        public async Task <Avatar> Update(int userId, Avatar avatar)
        {
            using (var context = new TradePMREntities())
            {
                var user = await context.Users.FindAsync(userId);

                if (user == null)
                {
                    throw new ApplicationException($"User with id {userId} not found");
                }

                if (user.AvatarId == null)
                {
                    avatar.Users.Add(user);
                    context.Avatars.Add(avatar);
                }
                else
                {
                    var result = await context.Avatars.FindAsync(user.AvatarId);

                    if (result == null)
                    {
                        return(null);
                    }
                    avatar.Id = result.Id;
                    context.Entry(result).CurrentValues.SetValues(avatar);
                    context.Entry(result).State = EntityState.Modified;
                }

                var updated = await context.SaveChangesAsync();

                if (updated == 0)
                {
                    return(null);
                }
                return(avatar);
            }
        }
Ejemplo n.º 5
0
        public async Task <bool> Delete(User user)
        {
            using (var context = new TradePMREntities())
            {
                var result = await context.Users.FindAsync(user.Id);

                if (result == null)
                {
                    return(false);
                }
                if (result.Avatar != null)
                {
                    context.Entry(result.Avatar).State = EntityState.Deleted;
                }
                context.Entry(result).State = EntityState.Deleted;
                var deleted = await context.SaveChangesAsync();

                if (deleted == 0)
                {
                    return(false);
                }
                return(true);
            }
        }
Ejemplo n.º 6
0
        public async Task <bool> Delete(int avatarId)
        {
            using (var context = new TradePMREntities())
            {
                var result = await context.Avatars.FindAsync(avatarId);

                if (result == null)
                {
                    return(false);
                }
                foreach (var user in result.Users)
                {
                    context.Entry(user).Entity.AvatarId = null;
                }
                context.Entry(result).State = EntityState.Deleted;
                var deleted = await context.SaveChangesAsync();

                if (deleted == 0)
                {
                    return(false);
                }
                return(true);
            }
        }