Esempio n. 1
0
        public async Task AddAsync(VehicleModel model, Guid userID)
        {
            Validate(model);

            if (model.Plate.Length != 8)
            {
                throw new CustomExceptions("Formato invalido de placa, deve ter no máximo 8 caracteres");
            }

            var company = await _userCompanyRepository.FirstOrDefaultAsync(x => x.UserID.Equals(userID.ToString()));

            if (await _vehicleRepository.AnyAsync(x => x.CompanyID == company.CompanyID && x.Plate.Equals(model.Plate)))
            {
                throw new CustomExceptions("Placa já cadastrada");
            }

            await _vehicleRepository.AddAsync(new Vehicle
            {
                Color       = model.Color,
                CompanyID   = company.CompanyID,
                CreatedDate = DateTimeOffset.UtcNow,
                ID          = Guid.NewGuid(),
                Make        = model.Make,
                Plate       = model.Plate,
                TypeID      = model.TypeID,
                Model       = model.Model,
            });

            await _vehicleRepository.SaveChangeAsync();
        }
Esempio n. 2
0
        private async Task <Guid> ValidateAsync(Guid userID, ParkingModel model)
        {
            if (userID == Guid.Empty)
            {
                throw new CustomExceptions("Não foi possível identificar o usuário");
            }
            else if (model == null)
            {
                throw new CustomExceptions("Não foi possível identificar a placa do veículo");
            }
            var userCompany = await _userCompanyRepository.FirstOrDefaultAsync(x => x.UserID.Equals(userID.ToString()));

            if (userCompany == null)
            {
                throw new CustomExceptions("Não foi possível encontrar o estacionamento");
            }

            if (!await _vehicleRepository.AnyAsync(x => x.CompanyID == userCompany.CompanyID && x.Plate.Equals(model.Plate)))
            {
                throw new CustomExceptions("A placa não pertence ao estacionamento");
            }

            return(userCompany.CompanyID);
        }