Пример #1
0
        public async Task <DinazorResult> AssignUserToUserGroup(long idUser, long idUserGroup)
        {
            var result = new DinazorResult();
            // check user id
            var exists = _userOperation.Exists(idUser);

            if (!exists.IsSuccess)
            {
                return(result);
            }
            if (!exists.Data)
            {
                result.Status = ResultStatus.NoSuchObject;
                return(result);
            }
            //check user group id
            exists = _userGroupOperation.Exists(idUserGroup);
            if (!exists.IsSuccess)
            {
                return(result);
            }
            if (!exists.Data)
            {
                result.Status = ResultStatus.NoSuchObject;
                return(result);
            }
            //so far so good
            using (var ctx = new DinazorContext())
            {
                //already added
                var alreadyAdded = ctx.RelUserUserGroup.Any(l => l.IdUser == idUser && l.IdUserGroup == idUserGroup);
                if (alreadyAdded)
                {
                    result.Status = ResultStatus.AlreadyAdded;
                    return(result);
                }
                RelUserUserGroup userUserGroup = new RelUserUserGroup();
                userUserGroup.IdUser      = idUser;
                userUserGroup.IdUserGroup = idUserGroup;

                var willBeAdded = ctx.RelUserUserGroup.Add(userUserGroup);
                try
                {
                    await ctx.SaveChangesAsync();

                    result.Success();
                    //result.ObjectId = willBeAdded;
                }
                catch (Exception e)
                {
                    result.Status = ResultStatus.UnknownError;
                }
            }
            return(result);
        }