protected override void ExecuteRequest(HttpContext context) { (bool isOk, User user) = CheckClaimsForUser(Request, context, _userRepository); if (!isOk || user == null) { return; } Skin skin = _skinRepository.Get(Request.Id).GetAwaiter().GetResult().FirstOrDefault(); if (skin != null) { Avatar avatar = _avatarRepository.GetByUser(user).GetAwaiter().GetResult(); if (avatar != null && avatar.Level >= skin.LevelMin) { avatar.SkinId = Request.Id; avatar = _avatarRepository.Update(avatar).GetAwaiter().GetResult(); if (avatar != null) { string avatarJson = JsonSerializer.Serialize(avatar); context.Response.StatusCode = StatusCodes.Status200OK; context.Response.WriteAsync(avatarJson).GetAwaiter().GetResult(); return; } } } context.Response.StatusCode = StatusCodes.Status400BadRequest; }
public Avatar Update(Avatar avatar) { if (avatar.Name.Length < 1) { throw new InvalidDataException("Name must be atleast 1 charecter"); } return(_avatarRepo.Update(avatar)); }
public async Task <IHttpActionResult> Update() { try { var userIdString = HttpContext.Current.Request.Form.Get("userId"); if (string.IsNullOrWhiteSpace(userIdString)) { return(BadRequest("User id is empty")); } if (!int.TryParse(userIdString, out int userId)) { return(BadRequest("User id is not a valid number")); } var newAvatar = new Avatar(); var avatar = HttpContext.Current.Request.Files.Get("avatar"); if (avatar != null) { if (avatar.ContentLength > 256000) { return(BadRequest("Avatar size exceeds 256K")); } if (!avatar.FileName.EndsWith(".jpg")) { return(BadRequest("Only .jpg extension is supported")); } byte[] avatarBytes; using (var ms = new MemoryStream()) { avatar.InputStream.CopyTo(ms); avatarBytes = ms.ToArray(); } newAvatar.Image = avatarBytes; } var avatarCreated = await _avatarRepository.Update(userId, newAvatar); if (avatarCreated == null) { return(BadRequest("Could not create avatar")); } return(Ok(avatarCreated)); } catch (Exception e) { Console.WriteLine(e); #if DEBUG return(InternalServerError(e)); #else return(InternalServerError()); #endif } }
public Avatar Update(Avatar avatar) { if (avatar.Name.Length < 1) { throw new InvalidDataException("Name must be atleast 1 char"); } if (avatar == null) { throw new InvalidDataException("Did not find avatar with id: " + avatar.Id); } return(_aRepo.Update(avatar)); }
public void UpdateAvatar(AvatarBll avatar) { if (avatar == null) { throw new ArgumentNullException("avatar"); } AvatarDal currentAvatar = avatar.ToDal(); AvatarDal existedAvatar = repository.GetById(avatar.Id); if (existedAvatar == null) { throw new EntityNotFoundException("avatar", avatar.Id); } existedAvatar.Image = currentAvatar.Image; repository.Update(existedAvatar); uow.Commit(); }
protected override void ExecuteRequest(HttpContext context) { (bool isOk, User user) = CheckClaimsForUser(Request, context, _userRepository); if (!isOk || user == null) return; Avatar avatar = _avatarRepository.GetByUser(user).GetAwaiter().GetResult(); if (avatar != null) { avatar.Size = Request.NewSize; avatar = _avatarRepository.Update(avatar).GetAwaiter().GetResult(); if (avatar != null) { string avatarJson = JsonSerializer.Serialize(avatar); context.Response.StatusCode = StatusCodes.Status200OK; context.Response.WriteAsync(avatarJson).GetAwaiter().GetResult(); } } else context.Response.StatusCode = StatusCodes.Status400BadRequest; }