public async Task <IActionResult> PutPost(int id, Post post) { if (id != post.PostId) { return(BadRequest()); } _context.Entry(post).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!PostExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public async Task <IActionResult> PutFollow(int id, Follow follow) { if (id != follow.AccountId) { return(BadRequest()); } _context.Entry(follow).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!FollowExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public async Task <ActionResult <Register> > PostFollow(Register register) { Account acc = new Account { AccountName = register.Username, HashedPassword = register.Password, Salt = "" }; using var trans = _context.Database.BeginTransaction(); try { _context.Account.Add(acc); await _context.SaveChangesAsync(); User user = new User { FullName = register.Fullname, Phone = register.Phonenumber, Email = register.Email, Dob = register.Dob, Gender = register.Gender, Username = register.Username, DateCreated = DateTime.Now, IsActive = true, IsAgency = false }; user.AccountId = acc.AccountId; _context.User.Add(user); await _context.SaveChangesAsync(); await trans.CommitAsync(); return(Ok()); } catch (Exception e) { e.Message.ToString(); await trans.RollbackAsync(); return(Conflict()); } }
public async Task <ActionResult> UploadPost(PostData post_data) { using (var trans = _context.Database.BeginTransaction()) { try { List <HashTag> tags = new List <HashTag>(); string[] tagslist = post_data.tags.Split(','); List <HashTag> existed_tags = _context.HashTag.Where(x => tagslist.Contains(x.TagName)).ToList(); List <int> tagg = existed_tags.Select(x => x.TagId).ToList(); //////////////////////////////////////////// foreach (string t in tagslist) { if (!existed_tags.Where(x => x.TagName == t).Any()) { tags.Add(new HashTag() { TagName = t }); } } foreach (HashTag h in tags) { _context.HashTag.Add(h); } await _context.SaveChangesAsync(); tagg = tagg.Union(tags.Select(x => x.TagId)).ToList(); ///////////////////////////////////////////////////////// Post post = new Post() { AccountId = post_data.AccountId, Description = post_data.Description, NumbFavorite = post_data.NumbFavorite, DateCreated = post_data.DateCreated, Checkin = post_data.Checkin, IsAds = post_data.IsAds }; _context.Post.Add(post); await _context.SaveChangesAsync(); ///////////////////////////////////////////// foreach (int h in tagg) { _context.PostTag.Add(new PostTag() { PostId = post.PostId, TagId = h }); } await _context.SaveChangesAsync(); ///////////////////////////////////////////////////// Image img = new Image() { ImageName = post_data.link, DateUploaded = post_data.DateCreated }; _context.Image.Add(img); _context.SaveChanges(); _context.PostImage.Add(new PostImage() { PostId = post.PostId, ImageId = img.ImageId }); await _context.SaveChangesAsync(); //////////////////////////////////////////////////////// await trans.CommitAsync(); } catch (Exception e) { e.Message.ToString(); await trans.RollbackAsync(); } } return(Ok()); }