public async Task Handle(CreatePhotoModel model)
        {
            throw new NotImplementedException();


            await _context.SaveChangesAsync();
        }
Exemplo n.º 2
0
        public async Task <Result <PhotoModel, Error> > Add(Guid activityId, CreatePhotoModel model)
        {
            model.UserId = Guid.Parse(_accessor.HttpContext.User.Claims.First(c => c.Type == "userId").Value);

            using var stream = new MemoryStream();
            await model.Image.CopyToAsync(stream);

            var activity = await _activitiesRepository.GetById(activityId);

            if (activity == null)
            {
                return(Result.Failure <PhotoModel, Error>(ErrorsList.UnavailableActivity));
            }

            var photo = new Photo(activityId, model.UserId, stream.ToArray());

            activity.AddPhoto(photo);

            var user = await _userRepository.GetById(photo.UserId);

            var notification = new Notification(activityId,
                                                DateTime.Now,
                                                $"{user.Username} has added a new photo to activity {activity.Name}."
                                                );

            activity.AddNotification(notification);

            _activitiesRepository.Update(activity);

            await _activitiesRepository.SaveChanges();

            return(Result.Success <PhotoModel, Error>(PhotoModel.Create(
                                                          photo.Id, photo.ActivityId, photo.UserId, user.Username, photo.Image)));
        }
Exemplo n.º 3
0
        public async Task <IActionResult> Add([FromRoute] Guid activityId, [FromForm] CreatePhotoModel model)
        {
            var(_, isFailure, value, error) = await _photosService.Add(activityId, model);

            if (isFailure)
            {
                return(BadRequest(error));
            }

            return(Created(value.Id.ToString(), null));
        }
Exemplo n.º 4
0
        public async Task <IActionResult> CreatePhoto([FromBody] CreatePhotoModel input)
        {
            logger.LogInformation($"{nameof(CreatePhoto)}({nameof(input.Filename)} = '{input.Filename}')");

            try
            {
                var photo = await photosService.CreatePhoto(GetCurrentUserId(), GetCurrentUserName(), input.Filename, input.Text);

                return(Json(photoModelConverter.ToPublic(photo)));
            }
            catch (Exception ex)
            {
                logger.LogError(ex, $"Error in {nameof(CreatePhoto)}({nameof(input.Filename)} = '{input.Filename}'):\n{ex.ToString()}");
                throw;
            }
        }
Exemplo n.º 5
0
        public ActionResult Create(CreatePhotoModel model, HttpPostedFileBase file)
        {
            var userId = User.Identity.GetUserId();

            if (userId == null)
            {
                return(RedirectToAction("GetAll"));
            }
            if (ModelState.IsValid)
            {
                // check image size
                if (Request.Files.Count > 0)
                {
                    file = Request.Files[0];
                    if (file != null && file.ContentLength > 0)
                    {
                        if (file.ContentLength <= 1048576)
                        {
                            var photo = Mapper.Map <CreatePhotoModel, Photo>(model);
                            photo.UserId = userId;

                            var status = _service.Add(photo, file);

                            if (string.IsNullOrWhiteSpace(status.ErrorMessage))
                            {
                                return(RedirectToAction("UserPhotos"));
                            }
                            else
                            {
                                ModelState.AddModelError("IsAnImage", status.ErrorMessage);
                            }
                        }
                        else
                        {
                            ModelState.AddModelError("Size", "Current image takes more than 1 MB");
                        }
                    }
                    else
                    {
                        ModelState.AddModelError("Empty", "Current image isn’t exist");
                    }
                }
            }
            var errors = ViewData.ModelState.Where(n => n.Value.Errors.Count > 0).ToList();

            return(View(model));
        }
Exemplo n.º 6
0
        public async Task <PhotoModel> Add(Guid offerId, CreatePhotoModel model)
        {
            var offer = await _repository.GetById(offerId);

            using var stream = new MemoryStream();
            await model.Content.CopyToAsync(stream);

            var photo = new Photo(model.Content.FileName, stream.ToArray());

            offer.Photos.Add(photo);

            _repository.Update(offer);

            await _repository.SaveChanges();

            return(_mapper.Map <PhotoModel>(photo));
        }
Exemplo n.º 7
0
        public async Task <PhotoModel> Add(Guid couponId, CreatePhotoModel model)
        {
            var userId = Guid.Parse(_accessor.HttpContext.User.Claims.First(c => c.Type == "userId").Value);
            var coupon = await _repository.GetById(couponId);

            await using var stream = new MemoryStream();
            await model.Content.CopyToAsync(stream);

            var photo = new Photo(model.Content.FileName, stream.ToArray(), userId);

            coupon.AddPhoto(photo);

            _repository.Update(coupon);
            await _repository.SaveChanges();

            return(_mapper.Map <PhotoModel>(photo));
        }
 public static CreatePhotoModel WithImageFile(this CreatePhotoModel model, IFormFile image)
 {
     model.Image = image;
     return(model);
 }
Exemplo n.º 9
0
        public async Task <IActionResult> Add([FromRoute] Guid couponId, [FromForm] CreatePhotoModel model)
        {
            var result = await _photosService.Add(couponId, model);

            return(Created(result.Id.ToString(), null));
        }
Exemplo n.º 10
0
        public async Task <IActionResult> Create([FromServices] CreatePhotoHandler handler, CreatePhotoModel model)
        {
            if (!ModelState.IsValid)
            {
                return(Json(false));
            }

            throw new NotImplementedException();
            //await handler.Handle(model);
            return(Json(true));
        }