Example #1
0
        public async Task SaveProfilePicture(PictureBindingModel profilePicture, ClaimsPrincipal user)
        {
            Picture profilePictureEntity = mapper.Map <Picture>(profilePicture);
            var     userDb = await Context.Users.FindAsync(user.Claims.First(c => c.Type == Constants.Claims.Id).Value);

            userDb.ProfilePicture = profilePictureEntity;
            Context.Users.Update(userDb);
            await Context.SaveChangesAsync();
        }
        public IHttpActionResult EscortAddPicture([FromBody] PictureBindingModel model)
        {
            if (model == null)
            {
                return(this.BadRequest("Missing picture data"));
            }

            if (!this.ModelState.IsValid)
            {
                return(this.BadRequest(this.ModelState));
            }

            var escortId = this.User.Identity.GetUserId();
            var escort   = this.EscortServiceData.Escorts
                           .FirstOrDefault(e => !e.IsDeleted && e.Id == escortId);

            if (escort == null && escortId != null)
            {
                return(this.Content(HttpStatusCode.Unauthorized, new
                {
                    Message = "User " + this.User.Identity.GetUserName() + " is not escort"
                }));
            }

            if (this.EscortServiceData.Pictures.Count(p => p.EscortId == escort.Id) > 10)
            {
                return(this.BadRequest("An escort allready has 10 pictures."));
            }

            if (this.EscortServiceData.Pictures.Any(p => p.B64 == model.B64))
            {
                return(this.Content(HttpStatusCode.Conflict,
                                    "This picture already exist"));
            }

            var newPicture = new Picture()
            {
                EscortId  = escort.Id,
                B64       = model.B64,
                IsProfile = false
            };

            this.EscortServiceData.Pictures.Add(newPicture);
            this.EscortServiceData.SaveChanges();

            return(this.Content(HttpStatusCode.OK,
                                string.Format("Picture with id: {0}   added successfully to escort: {1} pictures gallery.",
                                              newPicture.Id, escort.UserName)));
        }
        public async Task <IActionResult> ProfilePicture([FromBody] PictureBindingModel profilePicture)
        {
            if (!profilePicture.MediaType.EndsWith("jpg") && !profilePicture.MediaType.EndsWith("png"))
            {
                return(BadRequest(new ApiError("Unsupported file format! Allowed formats png and jpg")));
            }

            if (profilePicture.Value.Length > Constants.MaxProfilePictureSize)
            {
                return(BadRequest(new ApiError("ProfilePicture should be smaller than 4Mb")));
            }
            await fileService.SaveProfilePicture(profilePicture, User);

            return(Ok());
        }