Exemple #1
0
        public async Task <Battery> EnterNewBatteryAsync(int accountId, Battery battery)
        {
            Guard.AgainstNull(battery, "battery");
            Guard.AgainstAccountNumberMismatch(accountId, battery.AccountId, "accountId", "battery.AccountId");
            //Guard.AgainstNull(batteryType, "batteryType");

            // If the type does not exist, add it
            if (battery.BatteryType != null)
            {
                var repoBatteryType = await _batteryTypeRepository.GetByIdAsync(battery.BatteryType.Id);

                battery.BatteryType = repoBatteryType;
            }
            try
            {
                //battery.BatteryType = await _batteryTypeRepository.GetByIdAsync(battery.BatteryTypeId);    // Ensures the battery type is attached
                battery = await _batteryRepository.AddAsync(battery);

                _logger.LogInformation($"Added battery, new Id = {battery.Id}");
                return(battery);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, $"Error adding battery: {battery}.");
                return(null);
            }
        }
Exemple #2
0
        public async Task <Battery> EnterChargeDataAsync(int accountId, Battery battery, DateTime chargeDate, ChargeType chargeType, int mahUsed)
        {
            // Seems like we need a "discharge" object or event...
            // Perhaps this could be charge data... ??? not discharge. Then idea is that when you charge it, you know how much was added. Although if you put into storage mode that will not work...
            // Think about this based on the process used...
            // Charge, discharge, storage, repeat...

            Guard.AgainstNull(battery, "battery");
            Guard.AgainstNull(chargeDate, "chargeDate");


            // Make sure the battery exists
            var repoBattery = await _batteryRepository.GetByIdAsync(battery.Id);

            Guard.AgainstNull(repoBattery, "repoBattery");
            Guard.AgainstAccountNumberMismatch(accountId, repoBattery.AccountId, "accountId", "repoBattery.AccountId");

            var batteryCharge = new BatteryCharge
            {
                ChargedOn = chargeDate,
                Type      = chargeType,
                Mah       = mahUsed,
                Battery   = battery
            };

            await _batteryChargeRepository.AddAsync(batteryCharge);

            _logger.LogInformation($"Battery charge data saved, new Id = {batteryCharge.Id}");

            return(await _batteryRepository.GetByIdAsync(battery.Id));
        }
Exemple #3
0
        public async Task DeleteBatteryTypeAsync(int accountId, int id)
        {
            var batteryTypeToDelete = _batteryTypeRepository.GetById(id);

            Guard.AgainstBatteryTypeNotFound(batteryTypeToDelete, id, "batteryTypeToDelete");
            Guard.AgainstAccountNumberMismatch(accountId, batteryTypeToDelete.AccountId, "accountId", "batteryTypeToDelete.AccountId");
            await _batteryTypeRepository.DeleteAsync(batteryTypeToDelete);
        }
Exemple #4
0
        public async Task <ActionResult> GetRecent(int accountId)
        {
            try
            {
                Guard.AgainstAccountNumberMismatch(GetAccountIdClaim(), accountId.ToString(), "userClaim.accountId", "accountId");
                var flights = await _flightService.GetRecentFlightsAsync(accountId);

                return(Ok(flights));
            }
            catch (AccountConflictException)
            {
                return(Forbid());
            }
        }
Exemple #5
0
        // [Authorize]
        public async Task <ActionResult> ListActive(int accountId)
        {
            try
            {
                Guard.AgainstAccountNumberMismatch(GetAccountIdClaim(), accountId.ToString(), "userClaim.accountId", "accountId");
                var models = await _modelService.GetModelsByIsActiveAsync(accountId, true);

                return(Ok(models.ToArray()));
            }
            catch (AccountConflictException)
            {
                return(Forbid());
            }
        }
        public async Task <ActionResult> List(int accountId)
        {
            try
            {
                Guard.AgainstAccountNumberMismatch(GetAccountIdClaim(), accountId.ToString(), "userClaim.accountId", "accountId");
                var batteries = await _batteryService.ListBatteriesAsync(accountId);

                return(Ok(batteries.ToArray()));
            }
            catch (AccountConflictException)
            {
                return(Forbid());
            }
        }
Exemple #7
0
        public async Task <BatteryType> UpdateBatteryTypeAsync(int accountId, BatteryType batteryType)
        {
            Guard.AgainstNull(batteryType, "batteryType");
            Guard.AgainstAccountNumberMismatch(accountId, batteryType.AccountId, "accountId", "batteryType.AccountId");

            var result = await _batteryTypeRepository.UpdateAsync(batteryType);

            if (result != null)
            {
                _logger.LogInformation($"Updated battery type, Id = {batteryType.Id}");
            }
            else
            {
                _logger.LogWarning($"Could not update battery type, Id = {batteryType.Id}");
            }
            return(result);
        }
Exemple #8
0
        public async Task DeleteModelAsync(int accountId, int id)
        {
            try
            {
                var modelToDelete = _modelRepository.GetById(id);
                Guard.AgainstNull(modelToDelete, "modelToDelete");
                Guard.AgainstAccountNumberMismatch(accountId, modelToDelete.AccountId, "accountId", "modelToDelete.AccountId");

                await _modelRepository.DeleteAsync(modelToDelete);

                _logger.LogInformation($"Deleted model with Id: {id}");
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, $"Error deleting model with Id: {id}");
                throw;
            }
        }
Exemple #9
0
        public async Task <ActionResult <Location> > Post(int accountId, [FromBody] Location newLocation)
        {
            try
            {
                Guard.AgainstAccountNumberMismatch(GetAccountIdClaim(), accountId.ToString(), "userClaim.accountId", "accountId");
                var result = await locationService.AddLocationAsync(accountId, newLocation);

                return(Ok(result));
            }
            catch (AccountConflictException)
            {
                return(Forbid());
            }
            catch (Exception)
            {
                return(BadRequest("Error adding location"));
            }
        }
Exemple #10
0
        public async Task <Location> AddLocationAsync(int accountId, Location location)
        {
            Guard.AgainstNull(location, "location");
            Guard.AgainstAccountNumberMismatch(accountId, location.AccountId, "accountId", "location.AccountId");

            try
            {
                await _locationRepository.AddAsync(location);

                _logger.LogInformation($"Added location, new Id = {location.Id}");
                return(location);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, $"Error adding location: {location}.");
                return(null);
            }
        }
Exemple #11
0
        public async Task <ActionResult <ModelDto> > Post(int accountId, [FromBody] ModelDto newModel)
        {
            try
            {
                Guard.AgainstAccountNumberMismatch(GetAccountIdClaim(), accountId.ToString(), "userClaim.accountId", "accountId");
                var result = await _modelService.AddModelAsync(accountId, newModel);

                return(Ok(result));
            }
            catch (AccountConflictException)
            {
                return(Forbid());
            }
            catch (Exception)
            {
                return(BadRequest("Error adding model"));
            }
        }
Exemple #12
0
        public async Task <BatteryType> EnterNewBatteryTypeAsync(int accountId, BatteryType batteryType)
        {
            Guard.AgainstNull(batteryType, "batteryType");
            Guard.AgainstAccountNumberMismatch(accountId, batteryType.AccountId, "accountId", "batteryType.AccountId");

            try
            {
                await _batteryTypeRepository.AddAsync(batteryType);

                _logger.LogInformation($"Added battery type, new Id = {batteryType.Id}");
                return(batteryType);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, $"Error adding battery type: {batteryType}.");
                return(null);
            }
        }
Exemple #13
0
        public async Task DeleteFlightAsync(int accountId, int id)
        {
            try
            {
                var flightToDelete = _flightRepository.GetById(id);
                Guard.AgainstNull(flightToDelete, "flightToDelete");
                Guard.AgainstAccountNumberMismatch(accountId, flightToDelete.AccountId, "accountId", "flightToDelete.AccountId");

                await _flightRepository.DeleteAsync(flightToDelete);

                _logger.LogInformation($"Deleted flight with Id: {id}");
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, $"Error deleting flight with Id: {id}");
                throw;
            }
        }
        public async Task <ActionResult> GetById(int accountId, int id)
        {
            try
            {
                Guard.AgainstAccountNumberMismatch(GetAccountIdClaim(), accountId.ToString(), "userClaim.accountId", "accountId");
                var batteryType = await _batteryService.GetBatteryTypeByIdAsync(accountId, id);

                return(Ok(batteryType));
            }
            catch (ArgumentNullException)
            {
                return(NotFound($"Error finding battery type {id}"));
            }
            catch (AccountConflictException)
            {
                return(Forbid());
            }
        }
Exemple #15
0
        public async Task <ActionResult <List <FlightDto> > > GetByDateRange(int accountId, DateTime startDate, DateTime endDate)
        {
            try
            {
                Guard.AgainstAccountNumberMismatch(GetAccountIdClaim(), accountId.ToString(), "userClaim.accountId", "accountId");
                var flights = await _flightService.GetFlightsByDateAsync(accountId, startDate, endDate);

                return(Ok(flights));
            }
            catch (ArgumentNullException)
            {
                return(NotFound($"Error finding flights from {startDate} to {endDate}"));
            }
            catch (AccountConflictException)
            {
                return(Forbid());
            }
        }
Exemple #16
0
        public async Task <ActionResult <Battery> > Post(int accountId, [FromBody] Battery newBattery)
        {
            try
            {
                Guard.AgainstAccountNumberMismatch(GetAccountIdClaim(), accountId.ToString(), "userClaim.accountId", "accountId");
                var result = await _batteryService.EnterNewBatteryAsync(accountId, newBattery);

                return(Ok(result));
            }
            catch (AccountConflictException)
            {
                return(Forbid());
            }
            catch (Exception)
            {
                return(BadRequest("Error adding battery"));
            }
        }
Exemple #17
0
        public async Task <ActionResult <FlightDto> > GetById(int accountId, int id)
        {
            try
            {
                Guard.AgainstAccountNumberMismatch(GetAccountIdClaim(), accountId.ToString(), "userClaim.accountId", "accountId");
                var flight = await _flightService.GetFlightByIdAsync(accountId, id);

                return(Ok(flight));
            }
            catch (ArgumentNullException)
            {
                return(NotFound($"Error finding flight {id}"));
            }
            catch (AccountConflictException)
            {
                return(Forbid());
            }
        }
Exemple #18
0
        public async Task <ModelDto> UpdateModelAsync(int accountId, ModelDto model)
        {
            Guard.AgainstNull(model, "model");
            Guard.AgainstAccountNumberMismatch(accountId, model.AccountId, "accountId", "model.AccountId");

            var modelEntity = _mapper.Map <ModelDto, Model>(model);

            var result = await _modelRepository.UpdateAsync(modelEntity);

            if (result != null)
            {
                _logger.LogInformation($"Updated model, Id = {modelEntity.Id}");
            }
            else
            {
                _logger.LogWarning($"Could not update model, Id = {modelEntity.Id}");
            }
            return(await GetModelByIdAsync(accountId, modelEntity.Id));
        }
Exemple #19
0
        public async Task <FlightDto> UpdateFlightAsync(int accountId, FlightDto flight)
        {
            Guard.AgainstNull(flight, "flight");
            Guard.AgainstAccountNumberMismatch(accountId, flight.AccountId, "accountId", "flight.AccountId");

            var flightEntity = _mapper.Map <FlightDto, Flight>(flight);

            var result = await _flightRepository.UpdateAsync(flightEntity);

            if (result != null)
            {
                _logger.LogInformation($"Updated model, Id = {flightEntity.Id}");
            }
            else
            {
                _logger.LogWarning($"Could not update model, Id = {flightEntity.Id}");
            }
            return(await GetFlightByIdAsync(accountId, result.Id));
        }
Exemple #20
0
        public async Task <ModelDto> AddModelAsync(int accountId, ModelDto model)
        {
            Guard.AgainstNull(model, "model");
            Guard.AgainstAccountNumberMismatch(accountId, model.AccountId, "accountId", "model.AccountId");
            var modelEntity = _mapper.Map <ModelDto, Model>(model);

            try
            {
                await _modelRepository.AddAsync(modelEntity);

                _logger.LogInformation($"Added model, new Id = {modelEntity.Id}");
                return(await GetModelByIdAsync(accountId, modelEntity.Id));
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, $"Error adding model: {model}.");
                return(null);
            }
        }
        public async Task <ActionResult> AddNew(int accountId, [FromBody] BatteryType batteryType)
        {
            // Validate the input
            try
            {
                Guard.AgainstAccountNumberMismatch(GetAccountIdClaim(), accountId.ToString(), "userClaim.accountId", "accountId");
                var result = await _batteryService.EnterNewBatteryTypeAsync(accountId, batteryType);

                return(Ok(result));
            }
            catch (AccountConflictException)
            {
                return(Forbid());
            }
            catch (Exception)
            {
                return(BadRequest("Error adding battery type"));
            }
        }
Exemple #22
0
        public async Task <FlightDto> AddFlightAsync(int accountId, FlightDto flight)
        {
            Guard.AgainstNull(flight, "flight");
            Guard.AgainstAccountNumberMismatch(accountId, flight.AccountId, "accountId", "flight.AccountId");
            var flightEntity = _mapper.Map <FlightDto, Flight>(flight);

            try
            {
                flightEntity = await _flightRepository.AddAsync(flightEntity);

                _logger.LogInformation($"Added flight, new Id = {flightEntity.Id}");

                await UpdateFlightCountForModel(accountId, flightEntity.ModelId);

                return(await GetFlightByIdAsync(accountId, flightEntity.Id));
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, $"Error adding flight: {flight}.");
                throw;
            }
        }
Exemple #23
0
        public async Task <ActionResult <FlightDto> > Put(int accountId, [FromBody] FlightDto flight)
        {
            try
            {
                Guard.AgainstAccountNumberMismatch(GetAccountIdClaim(), accountId.ToString(), "userClaim.accountId", "accountId");
                var result = await _flightService.UpdateFlightAsync(accountId, flight);

                return(Ok(result));
            }
            catch (ArgumentNullException)
            {
                return(BadRequest("Error with input flight"));
            }
            catch (AccountConflictException)
            {
                return(Forbid());
            }
            catch (Exception)
            {
                return(BadRequest("Error updating flight"));
            }
        }
Exemple #24
0
        public async Task <ActionResult <FlightSummaryDto> > GetSummaryByModelAndDateRange(int accountId, int modelId, DateTime startDate, DateTime endDate)
        {
            try
            {
                Guard.AgainstAccountNumberMismatch(GetAccountIdClaim(), accountId.ToString(), "userClaim.accountId", "accountId");
                // Date guards
                Guard.AgainstNull(startDate, "startDate");
                Guard.AgainstNull(endDate, "endDate");

                var flights = await _flightService.GetFlightSummaryByModelAndDateRange(accountId, modelId, startDate, endDate);

                return(Ok(flights));
            }
            catch (ArgumentNullException)
            {
                return(NotFound($"Error finding flights for model {modelId}"));
            }
            catch (AccountConflictException)
            {
                return(Forbid());
            }
        }
Exemple #25
0
        public async Task <ActionResult <Battery> > Put(int accountId, [FromBody] Battery battery)
        {
            try
            {
                Guard.AgainstAccountNumberMismatch(GetAccountIdClaim(), accountId.ToString(), "userClaim.accountId", "accountId");
                var result = await _batteryService.UpdateBatteryAsync(accountId, battery);

                return(Ok(result));
            }
            catch (ArgumentNullException)
            {
                return(BadRequest("Error with input battery"));
            }
            catch (AccountConflictException)
            {
                return(Forbid());
            }
            catch (Exception)
            {
                return(BadRequest($"Error updating battery"));
            }
        }
Exemple #26
0
        public async Task <ActionResult> Delete(int accountId, int id)
        {
            try
            {
                Guard.AgainstAccountNumberMismatch(GetAccountIdClaim(), accountId.ToString(), "userClaim.accountId", "accountId");
                await _batteryService.DeleteBatteryAsync(accountId, id);

                return(Ok());
            }
            catch (BatteryNotFoundException)
            {
                return(NotFound($"Error finding battery {id} to delete"));
            }
            catch (AccountConflictException)
            {
                return(Forbid());
            }
            catch (Exception)
            {
                return(Conflict($"Error deleting battery {id}"));
            }
        }
Exemple #27
0
        public async Task <ActionResult> Delete(int accountId, int id)
        {
            try
            {
                Guard.AgainstAccountNumberMismatch(GetAccountIdClaim(), accountId.ToString(), "userClaim.accountId", "accountId");
                await _flightService.DeleteFlightAsync(accountId, id);

                return(Ok());
            }
            catch (ArgumentNullException)
            {
                return(NotFound($"Error finding flight {id} to delete"));
            }
            catch (AccountConflictException)
            {
                return(Forbid());
            }
            catch (Exception)
            {
                // Not expecting this to trigger as any flight can be deleted even if it has a model, location etc...
                return(Conflict("Error deleting flight {id}"));
            }
        }