/// <summary> /// Get single entity /// id == bikeId or "new" /// </summary> public BikeFormData GetById(int bikeId, Location?currentLocation) { using (var scope = Scope("GetById")) { AuthProvider.Authenticate(); // throws UnauthorizedException or we have CurrentUser after this // prepare Helper.Expect(typeof(Models.Bikes.Bike), bikeId); var isNew = bikeId <= 0; // authorize AuthProvider.Authorize(Permission.Bike_ViewAll); // process return(scope.Complete( () => new BikeFormData() { Bike = isNew ? new Models.Bikes.Bike(currentLocation) : new Models.Bikes.Bike(BikeManager.GetById(bikeId), currentLocation) }, t => $"Bike loaded with Id={t.Bike.BikeId}." )); } }
public async Task <Content <byte[]> > Get(ContentType contentType, string key, int seq) { using (var scope = Scope("Get")) { IContent content = null; switch (contentType) { case ContentType.BikeImage: case ContentType.BikeImageThumb: { AuthProvider.Authorize(Permission.Bike_ViewAll, Permission.Bike_Management); // TODO put down images using BikeId, so the Bike entity won't be needed here (hack: we use model name for the moment) var bike = BikeManager.GetById(int.Parse(key)); // uploaded image to bike if (bike.ImageFormat.HasValue && bike.ImageSeq.HasValue) { content = await ContentManager.GetContent(BikeManager.GetImageContentRef(bike, contentType == ContentType.BikeImageThumb), false); } if (content == null) { // fall back to model image content = await ContentManager.GetContent(new ContentRef(contentType, $"{bike.BikeModel.BikeModelName}.jpg"), true); } } break; default: throw new NotSupportedException($"Content type is not supported: {contentType}."); } return(scope.Complete( () => ContentManager.Serialize(content), t => $"Bike image loaded successfully (type: {contentType}, uri: {t.ContentRef.Uri})." )); } }