public HttpResponseMessage approveDraft(int id) { try { if (HttpContext.Current.Request.Cookies["sid"] == null) { throw new UnauthorizedAccessException("You have to have admin role to perform this action."); //TODO: strpati ovo u tijelo responsea } UserInfo userInfo = _authProvider.getAuth(HttpContext.Current.Request.Cookies["sid"].Value); if (!userInfo.Roles.Contains("ADMIN")) { throw new UnauthorizedAccessException("You have to have admin role to perform this action."); //TODO: strpati ovo u tijelo responsea } COMPONENTDRAFT cd = context.COMPONENTDRAFTs.Single(c => c.ID == id); if (cd == null) { throw new Exception("Component with specified ID not found."); } cd.APPROVED = "a"; if (cd.COMPONENTID == null) { CV_XML_FRAGMENT component = new CV_XML_FRAGMENT(); component.FRAGMENT_TYPE = cd.TYPE_ID; component.USER_ID = cd.USER_ID; component.XML_DATA = cd.DATA; context.CV_XML_FRAGMENT.Add(component); context.SaveChanges(); cd.CV_XML_FRAGMENT = component; } else { CV_XML_FRAGMENT component = context.CV_XML_FRAGMENT.Single(c => c.ID == cd.COMPONENTID); component.XML_DATA = cd.DATA; } context.SaveChanges(); return(new HttpResponseMessage(HttpStatusCode.OK)); } catch (UnauthorizedAccessException e) { return(new HttpResponseMessage(HttpStatusCode.Unauthorized)); } catch (Exception e) { return(new HttpResponseMessage(HttpStatusCode.NotFound)); //TODO make custom exceptions } }
public HttpResponseMessage saveDraft([FromBody] List <NewDraft> components) { try { if (HttpContext.Current.Request.Cookies["sid"] == null) { throw new UnauthorizedAccessException("You have to have be logged in to perform this action."); //TODO: strpati ovo u tijelo responsea } UserInfo userInfo = _authProvider.getAuth(HttpContext.Current.Request.Cookies["sid"].Value); int id = userInfo.UserId; foreach (var value in components) { // insert new draft with this type if (value.id == null) { CV_FRAGMENT_TYPE ft = context.CV_FRAGMENT_TYPE.Where(f => f.FRAGMENT_TYPE == value.title).FirstOrDefault(); COMPONENTDRAFT cd = new COMPONENTDRAFT(); cd.ADDITIONALINFO = value.additionalInfo; cd.USER_ID = id; cd.APPROVED = "w"; cd.TYPE_ID = ft.ID; XmlDocument doc = JsonConvert.DeserializeXmlNode(value.data, "root"); cd.DATA = doc.OuterXml; /* * CV_XML_FRAGMENT component = new CV_XML_FRAGMENT(); * component.FRAGMENT_TYPE = ft.ID; * component.USER_ID = id; * component.XML_DATA = "<empty></empty>"; * * context.CV_XML_FRAGMENT.Add(component); * context.SaveChanges(); * * cd.COMPONENTID = component.ID; */ context.COMPONENTDRAFTs.Add(cd); } // update else { COMPONENTDRAFT draft = context.COMPONENTDRAFTs.FirstOrDefault(c => c.ID == value.id); if (draft == null) { throw new Exception("Draft with specified id does not exist."); } XmlDocument doc = JsonConvert.DeserializeXmlNode(value.data, "root"); if (draft.DATA == doc.OuterXml) { continue; } if (value.additionalInfo != "") { draft.ADDITIONALINFO = value.additionalInfo; } draft.APPROVED = "w"; draft.DATA = doc.OuterXml; } } context.SaveChanges(); } catch (UnauthorizedAccessException e) { return(new HttpResponseMessage(HttpStatusCode.Unauthorized)); } catch (Exception e) { return(new HttpResponseMessage(HttpStatusCode.InternalServerError)); } return(new HttpResponseMessage(HttpStatusCode.Created)); }