Пример #1
0
        public async Task <FEUser> Create(FEUser newUser)
        {
            if (appDbContext.Users.FirstOrDefault(user => user.Email.ToLowerInvariant() == newUser.Email.ToLowerInvariant()) != null)
            {
                throw new InvalidOperationException(ErrorMessages.EmailTaken);
            }

            User userToAdd = new User {
                FullName    = newUser.FullName,
                PhoneNumber = newUser.PhoneNumber,
                Email       = newUser.Email,
                Password    = newUser.Password != null?PasswordCrypt.EncryptPassword(newUser.Password) : throw new InvalidOperationException(ErrorMessages.PasswordMissing),
                                        SlackId    = newUser.SlackId,
                                        MainOffice = newUser.MainOffice,
                                        IsDriver   = false,
                                        RoleId     = newUser.RoleId,
                                        Cars       = new List <Car>()
            };

            if (newUser.CarPlates != null && newUser.CarPlates.Length != 0)
            {
                UpdateCarPlates(ref userToAdd, newUser.CarPlates);
            }

            appDbContext.Users.Add(userToAdd);
            await appDbContext.SaveChangesAsync();

            newUser.Id = userToAdd.UserId;

            return(newUser);
        }
Пример #2
0
        public async Task <FEUser> Get(int userId)
        {
            var user = await appDbContext.Users.Include(u => u.Cars).FirstOrDefaultAsync(u => u.UserId == userId);

            if (user != null)
            {
                var userToReturn = new FEUser
                {
                    Id          = user.UserId,
                    FullName    = user.FullName,
                    PhoneNumber = user.PhoneNumber,
                    Email       = user.Email,
                    SlackId     = user.SlackId,
                    MainOffice  = user.MainOffice,
                    RoleId      = user.RoleId,
                    CarPlates   = user.Cars.Select(car => car.CarPlate).ToArray()
                };

                return(userToReturn);
            }
            else
            {
                throw new InvalidOperationException(ErrorMessages.UserDoesNotExist);
            }
        }
Пример #3
0
        public async Task <FEUser> Update(FEUser modifiedUser)
        {
            if (appDbContext.Users.FirstOrDefault(user => user.Email.ToLowerInvariant() == modifiedUser.Email.ToLowerInvariant() && user.UserId != modifiedUser.Id) != null)
            {
                throw new InvalidOperationException(ErrorMessages.EmailTaken);
            }

            var userToUpdate = appDbContext.Users.Include(user => user.Cars).FirstOrDefault(user => user.UserId == modifiedUser.Id);

            if (userToUpdate != null)
            {
                userToUpdate.FullName    = modifiedUser.FullName;
                userToUpdate.PhoneNumber = modifiedUser.PhoneNumber;
                userToUpdate.Email       = modifiedUser.Email;
                userToUpdate.SlackId     = modifiedUser.SlackId;
                userToUpdate.MainOffice  = modifiedUser.MainOffice;
                userToUpdate.RoleId      = modifiedUser.RoleId;
                UpdateCarPlates(ref userToUpdate, modifiedUser.CarPlates);

                appDbContext.Entry(userToUpdate).State = EntityState.Modified;
                appDbContext.Update(userToUpdate);

                await appDbContext.SaveChangesAsync();

                return(modifiedUser);
            }
            else
            {
                throw new InvalidOperationException(ErrorMessages.UserDoesNotExist);
            }
        }
Пример #4
0
        public async Task <ActionResult <FEUser> > Post([FromBody] FEUser newUser)
        {
            try
            {
                newUser = await adminRepository.Create(newUser);

                return(CreatedAtAction(nameof(GetAll), new { id = newUser.Id }, newUser)); // return all users after;
            }
            catch (ArgumentException e)
            {
                return(BadRequest(new Dictionary <string, string[]>
                {
                    { e.ParamName, new[] { e.Message.Split(Environment.NewLine)[0] } }
                }));
            }
            catch (Exception e)
            {
                return(StatusCode(500, e));
            }
        }
Пример #5
0
 public async Task <ActionResult <FEUser> > Put([FromBody] FEUser modifiedUser)
 {
     try
     {
         return(await adminRepository.Update(modifiedUser));
     }
     catch (InvalidOperationException e)
     {
         return(BadRequest(e));
     }
     catch (ArgumentException e)
     {
         return(BadRequest(new Dictionary <string, string[]>
         {
             { e.ParamName, new[] { e.Message.Split(Environment.NewLine)[0] } }
         }));
     }
     catch (Exception e)
     {
         return(StatusCode(500, e));
     }
 }