コード例 #1
0
            public async Task <Photo> Handle(Command request, CancellationToken cancellationToken)
            {
                var photoUploadResult = _photoAccessor.AddPhoto(request.File);
                var user = await _context.Users.SingleOrDefaultAsync(x =>
                                                                     x.UserName == _userAccessor.GetCurrentUsername());

                var photo = new Photo
                {
                    Url = photoUploadResult.Url,
                    Id  = photoUploadResult.PublicId
                };

                if (!user.Photos.Any(x => x.IsMain))
                {
                    photo.IsMain = true;
                }
                user.Photos.Add(photo);

                var success = await _context.SaveChangesAsync() > 0;

                if (success)
                {
                    return(photo);
                }

                throw new Exception("Problems saving changes!");
            }
コード例 #2
0
            public async Task <PhotoForReturnDto> Handle(Command request, CancellationToken cancellationToken)
            {
                var currentUser = await dataContext.Users.Include(u => u.UserPhotos).
                                  SingleOrDefaultAsync(u => u.UserName == userAccessor.GetCurrentUsername());

                if (currentUser == null)
                {
                    throw new RestException(HttpStatusCode.BadRequest, new { UserPhoto = "UnAuthorized" });
                }

                var uploadPhoto = await photosAccessor.AddPhoto(request.File);

                uploadPhoto.AppUserId = currentUser.Id;

                if (!currentUser.UserPhotos.Any(u => u.IsMain))
                {
                    uploadPhoto.IsMain = true;
                }

                dataContext.UserPhotos.Add(uploadPhoto);
                var success = await dataContext.SaveChangesAsync() > 0;

                if (success)
                {
                    var photoForReturn = mapper.Map <UserPhoto, PhotoForReturnDto>(uploadPhoto);
                    return(photoForReturn);
                }

                throw new Exception("Problem uplaod photo");
            }
コード例 #3
0
            public async Task <Unit> Handle(Command request, CancellationToken cancellationToken)
            {
                var photoUploadResult = _photosAccessor.AddPhoto(request.File);

                var sqlParams = new object[] { Guid.NewGuid(), request.ProductName, request.ProductPrice, true, request.ProductDescription, photoUploadResult.Url, request.CategoryId };

                var success = await _context.Database.ExecuteSqlRawAsync("CreateProduct @p0, @p1, @p2, @p3, @p4, @p5, @p6",
                                                                         sqlParams) == 1;

                if (success)
                {
                    return(Unit.Value);
                }
                throw new Exception("Problem saving changes");
            }