[HttpPost]//Grab the new firebase user and use it to create new AppUser //[Authorize] public void Post([FromBody] AppUser appUser) { var userClaims = User.Claims; var UserId = userClaims.Where(cl => cl.Type == "user_id").FirstOrDefault().Value; appUser.UniqueId = UserId; _dbContext.AppUsers.Add(appUser); _dbContext.SaveChanges(); }
public IActionResult Post(MateItem mateItem) { MateItem newmateItem = mateItem; var userClaims = User.Claims; var UserId = userClaims.Where(cl => cl.Type == "user_id").FirstOrDefault().Value; AppUser appUser = _dbContext.AppUsers.Where(appusr => appusr.UniqueId == UserId).FirstOrDefault(); var uploadsFolderPath = Path.Combine(_hostingEnv.WebRootPath, "Uploads"); if (!Directory.Exists(uploadsFolderPath)) { Directory.CreateDirectory(uploadsFolderPath); } //DONE: Find how to reduce file size(width & height), so that it will not have problem with xamarin Image control later. List <IFormFile> frmfiles = new List <IFormFile>(); if (mateItem.FirstImage != null && mateItem.FirstImage.Length > 0 && mateItem.FirstImage.Length < Utilities.MAX_FILE_SIZE) { if (Utilities.ACCEPTED_FILE_TYPES.Any(s => s == Path.GetExtension(mateItem.FirstImage.FileName))) { frmfiles.Add(mateItem.FirstImage); } } if (mateItem.SecondImage != null && mateItem.SecondImage.Length > 0 && mateItem.SecondImage.Length < Utilities.MAX_FILE_SIZE) { if (Utilities.ACCEPTED_FILE_TYPES.Any(s => s == Path.GetExtension(mateItem.SecondImage.FileName))) { frmfiles.Add(mateItem.SecondImage); } } if (mateItem.ThirdImage != null && mateItem.ThirdImage.Length > 0 && mateItem.ThirdImage.Length < Utilities.MAX_FILE_SIZE) { if (Utilities.ACCEPTED_FILE_TYPES.Any(s => s == Path.GetExtension(mateItem.ThirdImage.FileName))) { frmfiles.Add(mateItem.ThirdImage); } } if (frmfiles.Count > 0) { foreach (var item in frmfiles) { var firstImgFileName = Guid.NewGuid().ToString() + Path.GetExtension(item.FileName); var firstImgfilePath = Path.Combine(uploadsFolderPath, firstImgFileName); using (var img = Image.FromStream(item.OpenReadStream())) { Stream memStrm = new MemoryStream(img.Resize(300, 200).ToByteArray()); var filestrmRes = new FileStreamResult(memStrm, "image/jpg"); var stream = new FileStream(firstImgfilePath, FileMode.Create); try { memStrm.CopyTo(stream); //filestrmRes.FileStream.CopyTo(memStrm); } finally { stream.Dispose(); } } var newPhoto = new Photo { Filename = firstImgFileName }; newmateItem.ItemPhotos.Add(newPhoto); } } if (!ModelState.IsValid) { return(BadRequest("Submitted wrong data!")); } newmateItem.AppUserId = appUser.Id; //A bit redundant but the normal way is not working!!! newmateItem.Owner = _dbContext.AppUsers.Where(appusr => appusr.Id == newmateItem.AppUserId).FirstOrDefault(); _dbContext.MateItems.Add(newmateItem); //appUser.Items.Add(newmateItem); _dbContext.SaveChanges(); string newUri = Url.Link("GetMateItem", new { itemState = mateItem.ItemState, itemcategory = mateItem.ItemCategory, id = mateItem.Id }); /////////////////////Now send mail to admin(Puppy), notifying him to go and approve the new post ////////////////////////// NotifyAdminAndPostOwnerOfNewPost(newmateItem); ///////////////////End of send mail to admin(Puppy), notifying him to go and approve the new post/////////////////////// return(Created(newUri, mateItem)); //return Ok(mateItem); }