示例#1
0
        public async Task When_Create_NoLocationTypeId_Should_Fail()
        {
            var dto = new LocationBaseDto
            {
                Name = "New Location"
            };

            var commandResult = await _facade.Create(dto);

            Assert.That(commandResult.StatusCode, Is.EqualTo(FacadeStatusCode.BadRequest));
            Assert.That(commandResult.GeneratedId, Is.EqualTo(Guid.Empty));
            Assert.That(commandResult.ErrorCodes.Count, Is.EqualTo(1));
            Assert.That(commandResult.ErrorCodes[0].Code, Is.EqualTo(ValidationErrorCode.PropertyRequired("LocationTypeId").Code));
            Assert.That(commandResult.ErrorCodes[0].Description, Is.EqualTo(ValidationErrorCode.PropertyRequired("LocationTypeId").Description));
        }
示例#2
0
        public async Task <IHttpActionResult> GetConfig([FromUri] Guid?tenantId, [FromUri] Guid?operationId = null)
        {
            var errors = new List <FFErrorCode>();

            if (!tenantId.HasValue)
            {
                errors.Add(ValidationErrorCode.PropertyRequired(nameof(tenantId)));
            }

            if (errors.Any())
            {
                return(Request.CreateApiResponse(NoDtoHelpers.CreateCommandResult(errors)));
            }

            var authHeader = Request.Headers.Authorization.ToString();
            var result     = await _facade.Get((Guid)tenantId, operationId, authHeader);

            return(Request.CreateApiResponse(result));
        }
示例#3
0
        /// <summary>
        /// Deletes the requested operation if the operation has no measurements associated to it's locations.
        /// </summary>
        /// <param name="operationId">Guid identitifer of the operation to delete</param>
        /// <returns>Task that returns the request result.</returns>
        public async Task <CommandResultNoDto> Delete(Guid?operationId)
        {
            var errors = new List <FFErrorCode>();

            var userId = Thread.CurrentPrincipal == null ? null : Thread.CurrentPrincipal.GetUserIdFromPrincipal();

            if (string.IsNullOrEmpty(userId))
            {
                errors.Add(GeneralErrorCodes.TokenInvalid("UserId"));
            }

            if (!operationId.HasValue || operationId == Guid.Empty)
            {
                errors.Add(ValidationErrorCode.PropertyRequired("OperationId"));
            }

            if (errors.Count > 0)
            {
                return(NoDtoHelpers.CreateCommandResult(errors));
            }

            var userIdGuid = Guid.Parse(userId);

            List <Location> locations = new List <Location>();
            List <LocationParameterLimit> locationParameterLimits    = new List <LocationParameterLimit>();
            List <LocationParameter>      locationParameters         = new List <LocationParameter>();
            List <LocationLogEntry>       locationLocationLogEntries = new List <LocationLogEntry>();

            // Check that the Location exists and if it's an operation
            if (!_context.Locations.Any(x => x.Id == operationId.Value && x.LocationType.LocationTypeGroup.Id == Data.Constants.LocationTypeGroups.Operation.Id))
            {
                errors.Add(EntityErrorCode.EntityNotFound);
            }
            else
            {
                // Check if user is in the proper tenant.
                if (!_context.Locations.Single(x => x.Id == operationId.Value)
                    .ProductOfferingTenantLocations.Any(x => x.Tenant.Users.Any(u => u.Id == userIdGuid)))
                {
                    errors.Add(ValidationErrorCode.ForeignKeyValueDoesNotExist("TenantId"));
                }
                // Check that the operation has no measurements or notes and can be deleted

                // NOTE : This method will not scale beyond more than 4 or 5 levels max
                var operation = _context.Locations.Include("Locations").Single(x => x.Id == operationId.Value);
                locations.Add(operation);
                var systems = operation.Locations;
                locations.AddRange(systems);
                foreach (var system in systems)
                {
                    locations.AddRange(_context.Locations.Where(x => x.ParentId == system.Id).ToList());
                }

                var locationIds = locations.Select(l => l.Id);
                locationParameters =
                    _context.LocationParameters.Where(x => locationIds.Contains(x.LocationId)).ToList();

                var locationParamIds = locationParameters.Select(lp => lp.Id);
                locationParameterLimits =
                    _context.LocationParameterLimits.Where(
                        x => locationParamIds.Contains(x.LocationParameterId)).ToList();

                var hasLocationLocationLogEntries =
                    _context.LocationLogEntries.Any(x => locationIds.Contains(x.LocationId));

                var hasMeasurements   = _context.Measurements.Any(x => locationParamIds.Contains(x.LocationParameterId));
                var hasParameterNotes =
                    _context.LocationParameterNotes.Any(
                        x => locationParamIds.Contains(x.LocationParameterId));

                var hasMeasurementTransactions =
                    _context.MeasurementTransactions.Any(x => locationIds.Contains(x.LocationId));

                if (hasParameterNotes || hasMeasurements || hasLocationLocationLogEntries || hasMeasurementTransactions)
                {
                    errors.Add(EntityErrorCode.EntityCouldNotBeDeleted);
                }
            }

            if (errors.Count > 0)
            {
                return(NoDtoHelpers.CreateCommandResult(errors));
            }

            _context.LocationParameterLimits.RemoveRange(locationParameterLimits);
            _context.LocationParameters.RemoveRange(locationParameters);
            _context.Locations.RemoveRange(locations);
            _context.LocationLogEntries.RemoveRange(locationLocationLogEntries);

            await _context.SaveChangesAsync().ConfigureAwait(false);

            var commandResult = NoDtoHelpers.CreateCommandResult(errors);

            // CreateCommandResult will either return BadRequest(400) or Ok(200)
            // Overriding the Status code to return a 204 on a successful delete
            commandResult.StatusCode = FacadeStatusCode.NoContent;
            return(commandResult);
        }