public async Task<HttpResponseMessage> Post(int id) { try { var result = Request.Content.ReadAsFormDataAsync(); Stream streamIn = await Request.Content.ReadAsStreamAsync(); StreamReader streamReader = new StreamReader(streamIn); string jsonstring = streamReader.ReadToEnd(); var temp = JsonConvert.DeserializeObject<TagsInfo>(jsonstring); using (ResourcesDataContext dc = new ResourcesDataContext()) { foreach (var item in temp.tags) { var r = (from d in dc.bhdKeywords where d.isActive && d.value == item select d).FirstOrDefault(); if (r != null) { bhdResourceKeyword tmpResourceKeyword = new bhdResourceKeyword(); tmpResourceKeyword.KeywordId = r.id; tmpResourceKeyword.Resourceid = temp.ResourceId; dc.bhdResourceKeywords.InsertOnSubmit(tmpResourceKeyword); dc.SubmitChanges(); } else { bhdKeyword tmpkeyword = new bhdKeyword(); tmpkeyword.value = item; tmpkeyword.isActive = true; dc.bhdKeywords.InsertOnSubmit(tmpkeyword); dc.SubmitChanges(); bhdResourceKeyword tmpResourceKeyword = new bhdResourceKeyword(); tmpResourceKeyword.KeywordId = tmpkeyword.id; tmpResourceKeyword.Resourceid = temp.ResourceId; dc.bhdResourceKeywords.InsertOnSubmit(tmpResourceKeyword); dc.SubmitChanges(); } } } return Request.CreateResponse(HttpStatusCode.OK, "Success"); } catch (Exception) { return Request.CreateResponse(HttpStatusCode.BadRequest, "Something Broke. 400-1"); } }
public async Task<HttpResponseMessage> Post(int id) { try { var result = Request.Content.ReadAsFormDataAsync(); Stream streamIn = await Request.Content.ReadAsStreamAsync(); StreamReader streamReader = new StreamReader(streamIn); string jsonstring = streamReader.ReadToEnd(); // serialize that shit ResourceEditSend uploadModel; try { uploadModel = JsonConvert.DeserializeObject<ResourceEditSend>(jsonstring); } catch (Exception ex) { return Request.CreateResponse(HttpStatusCode.BadRequest, ex); } //update the information using (ResourcesDataContext dc = new ResourcesDataContext()) { bhdResource tmpResource = dc.bhdResources.Single((x) => x.id == id); tmpResource.name = uploadModel.resourceInfo.ResourceName; tmpResource.description = uploadModel.resourceInfo.ResourceDescription; tmpResource.topicId = uploadModel.resourceInfo.ResourceTopicId; tmpResource.languageId = uploadModel.resourceInfo.ResourceLanguageId; dc.SubmitChanges(); if (uploadModel.URL != "") { } dc.bhdResourceKeywords.DeleteAllOnSubmit<bhdResourceKeyword>(dc.bhdResourceKeywords.Where((r)=>r.Resourceid == id)); foreach (var item in uploadModel.tagsInfo.tags) { var r = (from d in dc.bhdKeywords where d.isActive && d.value == item select d).FirstOrDefault(); if (r != null) { bhdResourceKeyword tmpResourceKeyword = new bhdResourceKeyword(); tmpResourceKeyword.KeywordId = r.id; tmpResourceKeyword.Resourceid = tmpResource.id; dc.bhdResourceKeywords.InsertOnSubmit(tmpResourceKeyword); dc.SubmitChanges(); } else { bhdKeyword tmpkeyword = new bhdKeyword(); tmpkeyword.value = item; tmpkeyword.isActive = true; dc.bhdKeywords.InsertOnSubmit(tmpkeyword); dc.SubmitChanges(); bhdResourceKeyword tmpResourceKeyword = new bhdResourceKeyword(); tmpResourceKeyword.KeywordId = tmpkeyword.id; tmpResourceKeyword.Resourceid = tmpResource.id; dc.bhdResourceKeywords.InsertOnSubmit(tmpResourceKeyword); dc.SubmitChanges(); } } } } catch (Exception ex) { return Request.CreateResponse(HttpStatusCode.BadRequest, "Something Broke. " + ex.Message + ". 400-3"); } return Request.CreateResponse(HttpStatusCode.OK, "Success"); }