private async Task<Resource> BuildIssueResource(string relatedProjectName, IssueModel modelInstance) { var issueResource = new Issue(modelInstance.Id.ToString(), modelInstance.Title, modelInstance.Description, modelInstance.State); var resource = BuildSirenResource(issueResource, Rels.SingleIssueRelationType, res => { var opaqueUri = MakeUri<IssuesController>(c => c.DeleteIssue(relatedProjectName, modelInstance.Id)).AbsoluteUri; SetupRemoveIssueAction(res, opaqueUri); //open/close action var selfUri = MakeUri<IssuesController>(c => c.GetSingleIssue(relatedProjectName, modelInstance.Id)); var isOpen = res.Properties.State.Equals(OpenState); var action = isOpen ? CloseIssueAction(selfUri) : OpenIssueAction(selfUri); res.Actions.Add(action); }, res => { //list of comments var commentsRelatedUri = MakeUri<CommentsController>(c => c.GetComments(relatedProjectName, modelInstance.Id, null, null)); AddEntity(new List<string> { Rels.CollectionCommentsRelationType, Rels.CollectionRelationType }, res, Rels.CollectionRelationType, commentsRelatedUri); //add top level entity (project) var projectRelatedUri = MakeUri<ProjectsController>(c => c.FindSingleProject(relatedProjectName)); AddEntity(new List<string> { Rels.SingleProjectRelationType}, res, Rels.SingleProjectRelationType, projectRelatedUri); }, res => { var selfLink = MakeUri<IssuesController>(c => c.GetSingleIssue(relatedProjectName, modelInstance.Id)).AbsoluteUri; res.CreateNewLink(new[] { Rels.SelfRelationType }, selfLink); }); //add specfific element: tags var tagsRelated = await Context.IssueTagSet.Where(p => p.IssueId.ToString().Equals(resource.Properties.Id)).ToListAsync(); foreach (var tag in tagsRelated) { resource.Properties.Tags.Add(tag.TagName); } return resource; }
public async Task<HttpResponseMessage> CreateIssue(string projectName, WriteDocument template) { var body = template.Template; if (IsTemplateIncorrect(body)) { return CollectionTemplateInvalidResponse(); } var projectRootEntity = await Context.Projects.FindAsync(projectName); if (projectRootEntity == null) { return Request.ResourceNotFoundMessage(); } //BadRequest if state is not 'open' or 'closed' const int titleIdx = 0, stateIdx = 1, descriptionIdx = 2, tagsIdx = 3; if (!IsIssueStateValid(body.Data[stateIdx]?.Value)) { return BadIssueStateErrorMessage(body, stateIdx); } int? lastElementIndex = Context.Issues.Max(i => (int?)i.Id); //used in the making of the URI //Tags must belong to the project's set if (template.Template.Data.Count > GetTemplateParams()) { var tags = template.Template.Data[tagsIdx].Value.Split('+'); var tagModels = tags.Select(tag => new TagModel {Name = tag}).ToList(); //check if each tag is present in the ProjectTag set. if not, returns Error else associate it with the issue foreach (var tagModel in tagModels) { //verificar existencia no project relativo a este issue if (!await IsTagRelatedToProject(projectName, tagModel)) { return TagNotRelatedToProjectError(body.Data[tagsIdx].Name); } //caso contrario, associa tag com issue var elemIndex = (int)((lastElementIndex == null) ? 1 : lastElementIndex + 1); AssociationBetweenTagAndIssue(tagModel, elemIndex); } } //what to save var newResource = new IssueModel { Title = body.Data[titleIdx].Value, State = body.Data[stateIdx].Value, Description = body.Data[descriptionIdx].Value, ProjectModel = projectRootEntity }; Context.Issues.Add(newResource); Context.SaveChanges(); var issueCreated = await BuildIssueResource(projectName, newResource); var locationUri = MakeUri<IssuesController>(c => c.GetSingleIssue(projectName, lastElementIndex.Value)); return Request.BuildCreatedResourceResponse(issueCreated, locationUri); }