Example #1
0
 public CommonResult UpdateGroup(UpdateGroupRequest request)
 {
     return _groupsRepository.Update(request);
 }
Example #2
0
        public CommonResult Update(UpdateGroupRequest request)
        {
            using (var context = ContextFactory.GetDbContext(_dbConfiguration))
            {
                var group = context.Groups
                    .Include(x => x.Owner)
                    .FirstOrDefault(x => x.Id == request.Id);

                if (group == null)
                {
                    return CommonResult.Failure("Podana grupa nie istnieje.");
                }

                group.Name = request.Name.NotNullNorEmpty() ? request.Name : group.Name;
                group.Description = request.Description.NotNullNorEmpty() ? request.Description : group.Description;

                var wasPrivate = group.IsPrivate;

                if(!wasPrivate && request.IsPrivate && request.Password.NotNullNorEmpty())
                {
                    group.IsPrivate = request.IsPrivate;
                    group.Password = request.Password;

                    var userGroups = context.UserGroups.Where(x => x.User.Id != group.Owner.Id).ToList();

                    if (userGroups.Any())
                    {
                        context.Remove(userGroups);
                    }
                }

                if(wasPrivate && !request.IsPrivate)
                {
                    group.IsPrivate = request.IsPrivate;
                    group.Password = string.Empty;
                }

                context.SaveChanges();

                return CommonResult.Success();
            }
        }