public async Task <string> SetAppCenterDetailsAsync(BL_AppCenterDetails bl_dto) { var utcNow = DateTimeOffset.UtcNow; //Read Existing one var existingItem = await _repo.GetAppCenterDeviceDetails_DeviceId(bl_dto.DeviceId); if (existingItem == null) { existingItem = new AppCenterDeviceDetail { DTCreated = utcNow, DeviceId = bl_dto.DeviceId }; } //Map incoming data to existing entity (need to be mapped this way to not overwrite new properties not part of incoming BL_UserProfile) existingItem.DTLastUpdated = utcNow; existingItem.TypeOfDeviceOs = map.From(bl_dto.TypeOfOs); existingItem.UserprofileId = bl_dto.UserprofileId; //Validate - done after mapping to ensure the mapping logic not broken if (validator.CanSetAppCenterDetails(existingItem, out var validationResult)) { var idUpdatedOrCreated = await _repo.SetAppCenterDeviceDetailsAsync(existingItem); return(idUpdatedOrCreated); } else { throw new BusinessLogicException(validationResult); } }
public async Task <string> SetAppCenterDeviceDetailsAsync(AppCenterDeviceDetail entity) { SetEntityType(entity); //TODO: Consider setting TTL so these records are automatically deleted after a few weeks, so we don' send notifications to devices that are not in use any longer... - see https://docs.microsoft.com/en-us/azure/cosmos-db/time-to-live var upsert = await client.UpsertDocumentAsync(UriFactory.CreateDocumentCollectionUri(_settings.DatabaseId, _settings.DefaultCollectionId), entity); return(upsert.Resource.Id); }
public bool CanSetAppCenterDetails(AppCenterDeviceDetail entity, out ValidationResult results) { results = new ValidationResult(); if (entity == null) { results.Failures.Add(new ValidationFailure { Code = ValidationErrorCode.EntityIsNull, DebugMessage = $"Entity: {nameof(entity)} is null", }); return(false); } if (string.IsNullOrEmpty(entity.UserprofileId)) { results.Failures.Add(new ValidationFailure { Code = ValidationErrorCode.IdIsNull_UserProfileId, DebugMessage = $"Entity: {entity.GetType().Name} have Userprofile Id that is null", EntityJsonRepresentation = GetJson(entity), EntityIdInError = entity.Id, }); } if (string.IsNullOrEmpty(entity.DeviceId)) { results.Failures.Add(new ValidationFailure { Code = ValidationErrorCode.IdIsNull, DebugMessage = $"Entity: {entity.GetType().Name} have DeviceId that is null", EntityJsonRepresentation = GetJson(entity), EntityIdInError = entity.Id, }); } if (entity.TypeOfDeviceOs == DeviceOsTypeEnum.Unknown) { results.Failures.Add(new ValidationFailure { Code = ValidationErrorCode.IdIsNull, DebugMessage = $"Entity: {entity.GetType().Name} have Unknown DeviceOsType", EntityJsonRepresentation = GetJson(entity), EntityIdInError = entity.Id, }); } return(results.IsValid); }