public async Task <IActionResult> CreateBodyArea(BodyAreaForCreationDto BodyArea) { if (BodyArea != null) { var BodyMapped = _mapper.Map <BodyAreas>(BodyArea); _repo.Add(BodyMapped); await _repo.SaveAll(); return(Ok()); } return(BadRequest("Cheak Your Object")); }
public async Task <IActionResult> AddPhotoForUser(string userId, [FromForm] PhotoForCreationDto photoForCreationDto) { // cheack user id in rout same user id in token #endregion if (userId != User.FindFirst(ClaimTypes.NameIdentifier).Value) { return(Unauthorized()); } //get user data var userFromRepo = await _repo.GetUser(userId); // instans file var file = photoForCreationDto.File; var uploadResult = new ImageUploadResult(); // cheack any photo is comming if (file.Length > 0) { // create object to upload if have atlest one photo using (var stream = file.OpenReadStream()) { var uploadParams = new ImageUploadParams() { File = new FileDescription(file.Name, stream), Transformation = new Transformation().Width(500).Height(500) .Crop("fill") .Gravity("face") }; // upload photo uploadResult = _cloudinary.Upload(uploadParams); } } photoForCreationDto.Url = uploadResult.Uri.ToString(); photoForCreationDto.PublicId = uploadResult.PublicId; var photo = _mapper.Map <Photo>(photoForCreationDto); if (!userFromRepo.Photos.Any(u => u.IsMain)) { photo.IsMain = true; } userFromRepo.Photos.Add(photo); if (await _repo.SaveAll()) { var photoToReturn = _mapper.Map <PhotoForReturnDto>(photo); return(CreatedAtRoute("GetPhoto", new { id = photo.Id }, photoToReturn)); } return(BadRequest("Could Not add the photo")); }
public async Task <IActionResult> UpdateUser(string id, UserForUpdateDto userUpdateDto) { if (id != User.FindFirst(ClaimTypes.NameIdentifier).Value) { return(Unauthorized()); } var userFromRepo = await _repo.GetUser(id); _mapper.Map(userUpdateDto, userFromRepo); if (await _repo.SaveAll()) { return(NoContent()); } throw new Exception($"Updation User With {id} faild on save"); }
public async Task <IActionResult> CreateMessage(string userId, MessageForCreationDto messageForCreationDto) { var sender = await _rebo.GetUser(userId); if (sender.Id != User.FindFirst(ClaimTypes.NameIdentifier).Value) { return(Unauthorized()); } messageForCreationDto.SenderId = userId; var recipient = await _rebo.GetUser(messageForCreationDto.RecipientId); if (recipient == null) { return(BadRequest("Could Not Find User")); } var message = _mapper.Map <Message>(messageForCreationDto); _rebo.Add(message); if (await _rebo.SaveAll()) { var accesToken = Request.Headers["Authorization"]; var messageToSend = _mapper.Map <MessageToReturnDto>(message); // var theMessage = JsonConvert.SerializeObject(messageToSend); await _webSocketService.SendMessage(messageToSend, recipient.Id); var request = WebRequest.Create("https://onesignal.com/api/v1/notifications") as HttpWebRequest; request.KeepAlive = true; request.Method = "POST"; request.ContentType = "application/json; charset=utf-8"; request.Headers.Add("authorization", "Basic OGJjNGEzNGQtODMyZS00MmFkLThkMmUtY2I3ODllMzE1Zjdi"); byte[] byteArray = Encoding.UTF8.GetBytes("{" + "\"app_id\": \"1247426f-2d83-4c66-802e-8f8e2440b95f\"," + "\"contents\": {\"en\": messageToSend.Content}," + "\"included_segments\": [\"Active Users\"]}"); string responseContent = null; try { using (var writer = request.GetRequestStream()) { writer.Write(byteArray, 0, byteArray.Length); } using (var response = request.GetResponse() as HttpWebResponse) { using (var reader = new StreamReader(response.GetResponseStream())) { responseContent = reader.ReadToEnd(); } } } catch (WebException ex) { System.Diagnostics.Debug.WriteLine(ex.Message); System.Diagnostics.Debug.WriteLine(new StreamReader(ex.Response.GetResponseStream()).ReadToEnd()); } System.Diagnostics.Debug.WriteLine(responseContent); return(CreatedAtRoute("GetMessage", new { id = message.Id }, messageToSend)); } throw new Exception("Creating Message Failed on sent "); }