Esempio n. 1
0
        public Group AddGroup(Group group)
        {
            if (String.IsNullOrWhiteSpace(group.Code))
            {
                throw new RequireFieldException();
            }
            using (var userRepository = new BaseRepository<OldUser>())
            {
                var user = userRepository.GetAllItems.FirstOrDefault(x => x.Id == group.Curator);
                if (user == null)
                {
                    throw new UserDoesNotExistException();
                }
                if (user.Role == null)
                {
                    throw new UserRightsException();
                }
                if (String.Compare(user.Role.Name, Constraints.KCuratorRoleName, StringComparison.CurrentCulture) != 0)
                {
                    throw new UserRightsException();
                }
            }
            using (var groupRepository = new BaseRepository<Group>())
            {
                if (groupRepository.GetAllItems.Any(x => x.Curator == group.Curator && x.Code == group.Code))
                {
                    throw new GroupUniqueException();

                }
                group.CreationDate = DateTime.Now;
                if (!groupRepository.Create(group).Status)
                {
                    throw new CreateException();
                }
            }
            return group;
        }
Esempio n. 2
0
 public Object DeleteGroup(long Id)
 {
     var bodyResponse = new WebJsonResponse();
     try
     {
         var curator =
             _authorizationRepository.GetUserByCredentials(WebRequestHeaders.GetAuthorizationToken(this));
         if (curator == null)
         {
             return new HttpResponseMessage(HttpStatusCode.Unauthorized);
         }
         var group = new Group();
         group.Id = Id;
         group.Curator = curator.Id;
         _groupRepository.DeleteGroup(group);
         bodyResponse.Data = new object();
         bodyResponse.Status = true;
     }
     catch(UnauthorizedException)
     {
         return new HttpResponseMessage(HttpStatusCode.Unauthorized);
     }
     catch (Exception exception)
     {
         bodyResponse.Error = exception.Message;
     }
     return bodyResponse;
 }
Esempio n. 3
0
        public void UpdateGroup(Group group)
        {
            if (String.IsNullOrWhiteSpace(group.Code))
            {
                throw new RequireFieldException();
            }
            using (var userRepository = new BaseRepository<OldUser>())
            {
                if (!userRepository.GetAllItems.Any(x => x.Id == group.Curator))
                {
                    throw new UserDoesNotExistException();
                }
            }
            using (var groupRepository = new BaseRepository<Group>())
            {
                var updatedGroup = groupRepository.GetAllItems.FirstOrDefault(x => x.Id == group.Id);
                if (updatedGroup == null)
                {
                    throw new PersonsGroupDoesNotExistException();
                }
                if (updatedGroup.Curator != group.Curator)
                {
                    throw new UserRightsException();
                }
                if (String.Compare(updatedGroup.Code, group.Code, StringComparison.CurrentCulture) != 0
                    &&  groupRepository.GetAllItems.Any(x => x.Curator == group.Curator && x.Code == group.Code))
                {
                    throw new GroupUniqueException();
                }
                updatedGroup.Code = group.Code;
                updatedGroup.Note = group.Note;

                if (groupRepository.Update(updatedGroup).Status)
                {
                    return;
                }
                throw new UpdateException();
            }
        }
Esempio n. 4
0
 public bool DeleteGroup(Group group)
 {
     using (var groupRepository = new BaseRepository<Group>())
     {
         var deletedGroup = groupRepository.GetAllItems.FirstOrDefault(x => x.Id == group.Id);
         if (deletedGroup == null)
         {
             throw new PersonsGroupDoesNotExistException();
         }
         if (deletedGroup.Curator != group.Curator)
         {
             throw new UserRightsException();
         }
         if (groupRepository.Delete(deletedGroup).Status)
         {
             return true;
         }
         throw new DeleteException();
     }
 }