public async Task Post([FromBody] SaveLinkModel saveLink) { //eager loading user = appDbContext.Users.Include(u => u.MyFolders) .Select(u => u).Where(u => u.Email.Equals(User.Identity.Name)) .FirstOrDefault(); Link link = new Link { Body = saveLink.LinkBody, Description = saveLink.LinkDescription }; Folder folder = new Folder(); //if user don't have the folders if (user.MyFolders != null) { folder = user.MyFolders.Select(f => f).Where(f => f.Name.Equals(saveLink.FolderName)) .FirstOrDefault() ?? new Folder(); } //if user don't have the named folder if (folder.Name == null) { folder.Name = saveLink.FolderName; folder.AppUserId = user.Id; appDbContext.Folders.Add(folder); } link.FolderId = folder.Id; appDbContext.Links.Add(link); appDbContext.SaveChanges(); await Response.WriteAsync("Link successfully saved"); }
public async Task PutLink(int id, [FromBody] SaveLinkModel link) { Link dbLink = appDbContext.Links.Select(l => l) .Where(l => l.Id == id).FirstOrDefault(); if (dbLink == null) { await Response.WriteAsync("Link not found!"); return; } dbLink.Body = link.LinkBody; dbLink.Description = link.LinkDescription; appDbContext.Entry(dbLink).State = EntityState.Modified; appDbContext.SaveChanges(); await Response.WriteAsync("Link successfully changed"); }
public override void Run() { SaveLinkModel link = new SaveLinkModel(); Console.Write("Body: "); link.LinkBody = Console.ReadLine(); Console.Write("Description: "); link.LinkDescription = Console.ReadLine(); Console.Write("Folder Name: "); link.FolderName = Console.ReadLine(); using (var client = CreateClient(Commands.Token)) { var dataAsString = JsonConvert.SerializeObject(link); var content = new StringContent(dataAsString); content.Headers.ContentType = new MediaTypeHeaderValue("application/json"); var response = client.PostAsync(APP_PATH + "api/values", content).Result; ShowHttpStatus(response.StatusCode); } }