public async Task <IActionResult> UpdateRuuviStationByDeviceId(string id, RuuviStationCreateDto stationCreateDto)
        {
            var stationModel = this._mapper.Map <RuuviStation>(stationCreateDto);

            var stations = await GetAllObjectsAsync();

            var station = stations.Find(doc => doc.DeviceId == id);

            if (station != null)
            {
                stationModel.UpdatedAt = DateTime.UtcNow;
                stationModel.Id        = new ObjectId(id);
                stationModel.Tags.ForEach(tag => tag.UpdateAt = DateTime.UtcNow);

                this._repositoryRuuviStation.UpdateObject(id, stationModel);

                var ruuviStationReadDto = this._mapper.Map <RuuviStationReadDto>(station);
                var serviceAgreement    = new ServiceAgreementConfiguration(station, this._repositoryAgreement, this._serviceAgreementRepository);
                var serviceGeometric    = new ServiceGeometricConfiguration(station, this._repositoryGeometric, this._serviceGeometricRepository);

                List <ServiceAgreement> breachedAgreements = await serviceAgreement.IsBreachedCollection();

                List <ServiceGeometric> breachedGeometrics = await serviceGeometric.IsBreachedCollection();

                ruuviStationReadDto.ServiceAgreements = breachedAgreements;
                ruuviStationReadDto.ServiceGeometrics = breachedGeometrics;

                await this._hub.Clients.All.SendAsync("GetNewRuuviStations", ruuviStationReadDto);

                return(Ok(ruuviStationReadDto));
            }

            return(NotFound());
        }
        public async Task <IActionResult> CreateRuuviStation(RuuviStationCreateDto ruuviStationCreateDto)
        {
            var station = this._mapper.Map <RuuviStation>(ruuviStationCreateDto);

            station.Tags.ForEach(tag => tag.CreateDate = DateTime.UtcNow);
            station.Tags.ForEach(tag => tag.UpdateAt   = DateTime.UtcNow);
            station.Tags.ForEach(tag => tag.IsActive   = true);

            await this._repositoryRuuviStation.CreateObjectAsync(station);

            var ruuviStationReadDto = this._mapper.Map <RuuviStationReadDto>(station);
            var serviceAgreement    = new ServiceAgreementConfiguration(station, this._repositoryAgreement, this._serviceAgreementRepository);
            var serviceGeometric    = new ServiceGeometricConfiguration(station, this._repositoryGeometric, this._serviceGeometricRepository);

            List <ServiceAgreement> breachedAgreements = await serviceAgreement.IsBreachedCollection();

            List <ServiceGeometric> breachedGeometrics = await serviceGeometric.IsBreachedCollection();

            ruuviStationReadDto.ServiceAgreements = breachedAgreements;
            ruuviStationReadDto.ServiceGeometrics = breachedGeometrics;

            await this._hub.Clients.All.SendAsync("GetNewRuuviStations", ruuviStationReadDto);

            return(CreatedAtRoute(nameof(GetRuuviStationByDeviceId), new { Id = ruuviStationReadDto.Id }, ruuviStationReadDto));
        }
Beispiel #3
0
        public async Task <IActionResult> CreateRuuviStation(RuuviStationCreateDto ruuviStationCreateDto)
        {
            var station = _mapper.Map <RuuviStation>(ruuviStationCreateDto);

            station.Tags.ForEach(tag => tag.CreateDate = DateTime.UtcNow);
            station.Tags.ForEach(tag => tag.UpdateAt   = DateTime.UtcNow);
            station.Tags.ForEach(tag => tag.IsActive   = true);

            await _repository.CreateObjectAsync(station);

            var ruuviStationReadDto = _mapper.Map <RuuviStationReadDto>(station);

            // https://docs.microsoft.com/en-us/dotnet/api/system.web.http.apicontroller.createdatroute?view=aspnetcore-2.2
            return(CreatedAtRoute(nameof(GetRuuviStationByDeviceId), new { Id = ruuviStationReadDto.Id }, ruuviStationReadDto));
        }
Beispiel #4
0
        public async Task <IActionResult> UpdateRuuviStationByDeviceId(string id, RuuviStationCreateDto stationCreateDto)
        {
            var stationModel = _mapper.Map <RuuviStation>(stationCreateDto);

            var stations = await GetAllObjectsAsync();

            var station = stations.Find(doc => doc.DeviceId == id);

            if (station != null)
            {
                stationModel.UpdatedAt = DateTime.UtcNow;
                stationModel.Id        = new ObjectId(id);
                stationModel.Tags.ForEach(tag => tag.UpdateAt = DateTime.UtcNow);

                _repository.UpdateObject(id, stationModel);
                return(Ok(_mapper.Map <RuuviStationReadDto>(stationModel)));
            }

            return(NotFound());
        }