private double calculate(IEnumerable <LocationLogEntry> entries)
            {
                double result = 0.0;
                // No worries in taking the first element - since its difference in distance with the first element is 0.0(plus minus rounding)
                //  Yes, it's a waste of CPU cycles, but it's more readable
                LocationLogEntry previous = entries.FirstOrDefault();

                foreach (var ll in entries)
                {
                    result  += distanceCalculation(Math.Abs(previous.Latitude - ll.Latitude), Math.Abs(previous.Longitude - ll.Longitude));
                    previous = ll;
                }

                return(result);
            }
Exemple #2
0
        /// <summary>
        /// Creates a Location Log Entry.
        /// </summary>
        /// <param name="dto">Data Transfer Object (DTO) used to create a Location Log Entry.</param>
        /// <returns>
        /// An asynchronous task result containing information needed to create an API response message.
        /// If successful, the task result contains the DTO associated with the Location Log Entry.
        /// </returns>
        public override async Task <CommandResult <LocationLogEntryQueryDto, Guid> > Create(LocationLogEntryBaseDto dto)
        {
            // User ID should always be available, but if not ...
            var userId = GetCurrentUser();

            if (!userId.HasValue)
            {
                return(Command.Error <LocationLogEntryQueryDto>(GeneralErrorCodes.TokenInvalid("UserId")));
            }

            var validationResponse = ValidatorCreate.Validate(dto);

            if (dto == null)
            {
                return(Command.Error <LocationLogEntryQueryDto>(validationResponse));
            }

            if (dto.Id != Guid.Empty)
            {
                validationResponse.FFErrors.Add(ValidationErrorCode.PropertyIsInvalid(nameof(LocationLogEntry.Id)));
            }

            var locationExists = await DoesLocationExist(dto.LocationId);

            if (!locationExists)
            {
                validationResponse.FFErrors.Add(ValidationErrorCode.ForeignKeyValueDoesNotExist(nameof(LocationLogEntry.LocationId)));
            }

            if (validationResponse.IsInvalid)
            {
                return(Command.Error <LocationLogEntryQueryDto>(validationResponse));
            }

            var locationLogEntry = new LocationLogEntry
            {
                Id = Guid.NewGuid()
            };

            _mapper.Map(dto, locationLogEntry);

            locationLogEntry.SetAuditFieldsOnCreate(userId.Value);

            _context.LocationLogEntries.Add(locationLogEntry);
            await _context.SaveChangesAsync().ConfigureAwait(false);

            return(Command.Created(_mapper.Map(locationLogEntry, new LocationLogEntryQueryDto()), locationLogEntry.Id));
        }