Esempio n. 1
0
        /// <summary>
        /// Create new entity
        /// </summary>
        public Models.Bikes.Bike Post(Models.Bikes.Bike bike, Location?currentLocation)
        {
            using (var scope = Scope("Post"))
            {
                // authorize
                AuthProvider.Authorize(Permission.Bike_Management); // throws UnauthorizedException or we have CurrentUser after this

                // prepare
                bike.Created          = DateTime.UtcNow;
                bike.CreatedBy        = new UserRef(AuthProvider.CurrentUser);
                bike.AvailableFromUtc = DateTime.UtcNow;
                Helper.Expect(bike);

                // validate
                Helper.ValidateModel(bike, true);

                // process
                var saveImage = !string.IsNullOrEmpty(bike.ImageToUploadContentBase64) && !string.IsNullOrEmpty(bike.ImageToUploadFileName);
                var entity    = bike.ToEntity();

                if (saveImage)
                {
                    // increment seq
                    entity.ImageSeq    = 1;
                    entity.ImageFormat = ContentManager.GetFormat(BikeManager.GetImageContentRef(entity, false)).FileFormat.GetValueOrDefault(); // image will be validated when saving
                }

                BikeManager.Add(entity);

                return(scope.Complete(
                           () =>
                {
                    // after commit
                    if (saveImage)
                    {
                        // save new image
                        BikeManager.SaveImage(entity, ContentManager.DecodeBase64Image(bike.ImageToUploadContentBase64));
                    }

                    return new Models.Bikes.Bike(BikeManager.GetById(entity.BikeId), currentLocation);
                },
                           t => $"Bike has been created with Id={bike.BikeId}."
                           ));
            }
        }
Esempio n. 2
0
        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})."
                           ));
            }
        }