Exemplo n.º 1
0
 private void ValidateSubscription(int profileId, int subscriptionProfileId)
 {
     if (subscriptionProfileId == profileId)
     {
         throw new ServiceException("Profile can not subscribe to itself.")
               {
                   Type = ExceptionType.SameException,
               }
     }
     ;
     DAL.Entities.Profile profile = _db.ProfileRepository.Get(profileId);
     if (profile == null)
     {
         throw new ServiceException($"Profile with id = {profileId} was not found!")
               {
                   Type = ExceptionType.ForeignKeyException
               }
     }
     ;
     if (!_db.ProfileRepository.Contains(subscriptionProfileId))
     {
         throw new ServiceException($"Profile with id = {subscriptionProfileId} was not found!")
               {
                   Type = ExceptionType.ForeignKeyException
               }
     }
     ;
     if (GetProfileSubscriptionsByProfile(profile)
         .Select(x => x.Id)
         .Contains(subscriptionProfileId))
     {
         throw new ServiceException(ExceptionType.UniqueException,
                                    $"Profile already has a subscription with id = {subscriptionProfileId}");
     }
 }
Exemplo n.º 2
0
        public async Task CreateAsync(UserDTO userDto, int?customerId = 0)
        {
            validateService.Validate(userDto);
            UserEntity user = await db.UserManager.FindByNameAsync(userDto.Login);

            if (user == null)
            {
                user = new UserEntity {
                    Email = userDto.Login, UserName = userDto.Login
                };
                var result = await db.UserManager.CreateAsync(user, userDto.Password);

                if (result.Errors.Any())
                {
                    throw new ValidationException(result.Errors.FirstOrDefault(), "");
                }

                await db.UserManager.AddToRoleAsync(user.Id, userDto.Role);

                var clientProfile = new Entity.Profile {
                    Id = user.Id, Login = userDto.Login
                };

                if (customerId != 0)
                {
                    AddProfileToCustomer(clientProfile, customerId);
                    await db.UserManager.AddToRoleAsync(user.Id, "Employee");
                }
                db.Profiles.Create(clientProfile);
            }
            else
            {
                throw new ValidationException(Resource.Resource.UserAlreadyExists, "Login");
            }
        }
Exemplo n.º 3
0
        public int UpdateOrCreateProfile(ProfileDTO profile)
        {
            if (profile == null)
            {
                throw new ServiceException(ExceptionType.NullException, "Profile is null");
            }
            var uniqueProfile = _db.ProfileRepository.GetAll()
                                .FirstOrDefault(x => x.UserId == profile.UserId);

            if (uniqueProfile != null && uniqueProfile.Id != profile.Id)
            {
                throw new ServiceException(ExceptionType.UniqueException,
                                           $"User (with id = {profile.UserId}) already has a profile!");
            }
            if (profile.Id < 1)
            {
                return(AddProfile(profile));
            }
            DAL.Entities.Profile profileDAL = _db.ProfileRepository.Get(profile.Id);
            if (profileDAL == null)
            {
                return(AddProfile(profile));
            }
            ValidateProfile(profile);
            profileDAL.BirthDate   = profile.BirthDate;
            profileDAL.Description = profile.Description;
            profileDAL.Name        = profile.Name;
            profileDAL.ImageFileId = profile.ImageFileId;
            _db.Save();
            return(profile.Id);
        }
        public bool CreateProfile(Profile profile)
        {
            if (profile.ID != 0)
                return false;

            context.Profiles.Add(profile);
            context.SaveChanges();
            return true;
        }
Exemplo n.º 5
0
 private HashSet <ProfileDTO> GetProfileSubscriptionsByProfile(DAL.Entities.Profile profile)
 {
     return(_mapper.Map <IEnumerable <DAL.Entities.Profile>, HashSet <ProfileDTO> >
            (
                profile?
                .Subscriptions
                .Select(x => x.SubscriptionProfile)
            ));
 }
Exemplo n.º 6
0
 public ImageFileDTO GetImageFileByProfileId(int profileId)
 {
     DAL.Entities.Profile profile = _db.ProfileRepository.Get(profileId);
     if (profile == null)
     {
         throw new ServiceException(ExceptionType.NotFoundException,
                                    $"Profile with id = {profileId} was not found!");
     }
     return(_mapper.Map <ImageFileDTO>(profile.ImageFile));
 }
Exemplo n.º 7
0
 public HashSet <ProfileDTO> GetProfileSubscriptions(int profileId)
 {
     DAL.Entities.Profile profile = _db.ProfileRepository.Get(profileId);
     if (profile == null)
     {
         throw new ServiceException(ExceptionType.NotFoundException,
                                    $"Profile with id = {profileId} was not found!");
     }
     return(GetProfileSubscriptionsByProfile(profile));
 }
 public bool UpdateProfile(Profile profile, int userId)
 {
     var oldProfile = (from p in context.Profiles
                      where p.UserID == userId
                      select p).FirstOrDefault();
     if (oldProfile != null)
         context.Profiles.Remove(oldProfile);
     context.Profiles.Add(profile);
     context.SaveChanges();
     return true;
 }
Exemplo n.º 9
0
 public HashSet <ProfileDTO> GetProfileSubscribers(int profileId)
 {
     DAL.Entities.Profile profile = _db.ProfileRepository.Get(profileId);
     if (profile == null)
     {
         throw new ServiceException(ExceptionType.NotFoundException,
                                    $"Profile with id = {profileId} was not found!");
     }
     return(_mapper.Map <IEnumerable <DAL.Entities.Profile>, HashSet <ProfileDTO> >
                (profile.Subscribers.Select(x => x.SubscriberProfile)));
 }
 public void Create(ProfileDTO profile)
 {
     DAL.Entities.Profile newProfile = new DAL.Entities.Profile
     {
         FirstName = profile.FirstName,
         LastName = profile.LastName,
         Age = profile.Age,
         CreationDate = DateTime.Now,
         UserId = profile.UserId
     };
     Database.Profiles.Create(newProfile);
     Database.Save();
 }
 public void Create(ProfileDTO profile)
 {
     DAL.Entities.Profile newProfile = new DAL.Entities.Profile
     {
         FirstName    = profile.FirstName,
         LastName     = profile.LastName,
         Age          = profile.Age,
         CreationDate = DateTime.Now,
         UserId       = profile.UserId
     };
     Database.Profiles.Create(newProfile);
     Database.Save();
 }
Exemplo n.º 12
0
 public void AddProfileToCustomer(Entity.Profile profile, int?customerId)
 {
     if (profile == null)
     {
         throw new ValidationException(Resource.Resource.ProfileNullReference, "");
     }
     if (customerId == null)
     {
         throw new ValidationException(Resource.Resource.CustomerIdNotSet, "");
     }
     if (!db.Customers.IsExist(customerId.Value))
     {
         throw new ValidationException(Resource.Resource.ProfileNotFound, "");
     }
     db.Customers.AddProfileToCustomer(profile, customerId.Value);
 }
Exemplo n.º 13
0
 public int AddProfile(ProfileDTO profile)
 {
     ValidateProfile(profile);
     if (_db.ProfileRepository.GetAll().Any(x => x.UserId == profile.UserId))
     {
         throw new ServiceException($"User (with id = {profile.UserId}) already has a profile!")
               {
                   Type           = ExceptionType.UniqueException,
                   ExceptionValue = nameof(profile.UserId)
               }
     }
     ;
     DAL.Entities.Profile profileDAL = _db.ProfileRepository
                                       .Add(_mapper.Map <DAL.Entities.Profile>(profile));
     _db.Save();
     return(profileDAL.Id);
 }
Exemplo n.º 14
0
        public bool RemoveProfileSubscription(int subscriptionProfileId, int profileId)
        {
            DAL.Entities.Profile profile = _db.ProfileRepository.Get(profileId);
            if (profile == null)
            {
                throw new ServiceException(ExceptionType.NotFoundException,
                                           $"Profile with id = {profileId} was not found!");
            }
            int?subscriptionId = profile.Subscriptions
                                 .FirstOrDefault(x => x.SubscriptionProfileId == subscriptionProfileId)?
                                 .Id;

            if (subscriptionId == null)
            {
                return(false);
            }
            _db.SubscriptionRepository.Delete(subscriptionId.Value);
            _db.Save();
            return(true);
        }
        public void Create(UserDTO user)
        {
            User u = new User
            {
                Email = user.Email,
                Password = user.Password,
                RoleId = user.RoleId
            };
            Database.Users.Create(u);
            Database.Save();

            DAL.Entities.Profile prof = new DAL.Entities.Profile
            {
                 FirstName = "Undefined",
                 LastName = "Undefined",
                 Age = 0,
                 CreationDate = DateTime.Now,
                 UserId = u.Id
            };
            Database.Profiles.Create(prof);
            Database.Save();
        }
        public void Create(UserDTO user)
        {
            User u = new User
            {
                Email    = user.Email,
                Password = user.Password,
                RoleId   = user.RoleId
            };

            Database.Users.Create(u);
            Database.Save();

            DAL.Entities.Profile prof = new DAL.Entities.Profile
            {
                FirstName    = "Undefined",
                LastName     = "Undefined",
                Age          = 0,
                CreationDate = DateTime.Now,
                UserId       = u.Id
            };
            Database.Profiles.Create(prof);
            Database.Save();
        }