protected override IMedia Update(int id, MediaRepresentation content) { var found = MediaService.GetById(id); if (found == null) { throw new HttpResponseException(HttpStatusCode.NotFound); } //Validate properties var validator = new ContentPropertyValidator <IMedia>(ModelState, Services.DataTypeService); validator.ValidateItem(content, found); if (!ModelState.IsValid) { throw ValidationException(ModelState, content, id: id); } Mapper.Map(content, found); MediaService.Save(found); return(found); }
protected override IMedia CreateNew(MediaRepresentation content) { //we cannot continue here if the mandatory items are empty (i.e. name, etc...) if (!ModelState.IsValid) { throw ValidationException(ModelState, content); } var contentType = Services.ContentTypeService.GetMediaType(content.ContentTypeAlias); if (contentType == null) { ModelState.AddModelError("content.contentTypeAlias", "No media type found with alias " + content.ContentTypeAlias); throw ValidationException(ModelState, content); } //create an item before persisting of the correct content type var created = MediaService.CreateMedia(content.Name, content.ParentId, content.ContentTypeAlias, Security.CurrentUser.Id); //Validate properties var validator = new ContentPropertyValidator <IMedia>(ModelState, Services.DataTypeService); validator.ValidateItem(content, created); if (!ModelState.IsValid) { throw ValidationException(ModelState, content); } Mapper.Map(content, created); MediaService.Save(created); return(created); }
public async Task <HttpResponseMessage> Post(MediaRepresentation content) { if (content == null) { return(Request.CreateResponse(HttpStatusCode.NotFound)); } //We need to do the INT lookup from a GUID since the INT is what governs security there's no way around this right now var intParentId = Services.EntityService.GetIdForKey(content.ParentId, UmbracoObjectTypes.Media); if (!intParentId) { return(Request.CreateResponse(HttpStatusCode.NotFound)); } if (!await AuthorizationService.AuthorizeAsync(ClaimsPrincipal, new ContentResourceAccess(intParentId.Result), AuthorizationPolicies.MediaCreate)) { return(Request.CreateResponse(HttpStatusCode.Unauthorized)); } try { //we cannot continue here if the mandatory items are empty (i.e. name, etc...) if (!ModelState.IsValid) { throw ValidationException(ModelState, content, LinkTemplates.Media.Root); } var contentType = Services.ContentTypeService.GetMediaType(content.ContentTypeAlias); if (contentType == null) { ModelState.AddModelError("content.contentTypeAlias", "No media type found with alias " + content.ContentTypeAlias); throw ValidationException(ModelState, content, LinkTemplates.Media.Root); } //create an item before persisting of the correct content type var created = Services.MediaService.CreateMedia(content.Name, content.ParentId, content.ContentTypeAlias, ClaimsPrincipal.GetUserId() ?? 0); //Validate properties var validator = new ContentPropertyValidator <IMedia>(ModelState, Services.DataTypeService); validator.ValidateItem(content, created); if (!ModelState.IsValid) { throw ValidationException(ModelState, content, LinkTemplates.Media.Root); } Mapper.Map(content, created); Services.MediaService.Save(created, ClaimsPrincipal.GetUserId() ?? 0); var msg = Request.CreateResponse(HttpStatusCode.Created, Mapper.Map <MediaRepresentation>(created)); AddLocationResponseHeader(msg, LinkTemplates.Media.Self.CreateLink(new { id = created.Id })); return(msg); } catch (ModelValidationException exception) { return(Request.CreateResponse(HttpStatusCode.BadRequest, exception.Errors)); } }
public async Task <HttpResponseMessage> Put(int id, ContentRepresentation content) { if (content == null) { return(Request.CreateResponse(HttpStatusCode.NotFound)); } //TODO: Since this Id is based on a route parameter it should be possible to authz this with an attribute if (!await AuthorizationService.AuthorizeAsync(ClaimsPrincipal, new ContentResourceAccess(id), AuthorizationPolicies.ContentUpdate)) { return(Request.CreateResponse(HttpStatusCode.Unauthorized)); } try { var found = Services.ContentService.GetById(id); if (found == null) { throw new HttpResponseException(HttpStatusCode.NotFound); } //Validate properties var validator = new ContentPropertyValidator <IContent>(ModelState, Services.DataTypeService); validator.ValidateItem(content, found); if (!ModelState.IsValid) { throw ValidationException(ModelState, content, LinkTemplates.Content.Self, id: id); } Mapper.Map(content, found); if (!content.Published) { //if the flag is not published then we just save a draft Services.ContentService.Save(found, ClaimsPrincipal.GetUserId() ?? 0); } else { //publish it if the flag is set, if it's already published that's ok too var result = Services.ContentService.SaveAndPublishWithStatus(found, ClaimsPrincipal.GetUserId() ?? 0); if (!result.Success) { SetModelStateForPublishStatus(result.Result); throw ValidationException(ModelState, content, LinkTemplates.Content.Self, id: id); } } var rep = Mapper.Map <ContentRepresentation>(found); return(Request.CreateResponse(HttpStatusCode.OK, rep)); } catch (ModelValidationException exception) { return(Request.CreateResponse(HttpStatusCode.BadRequest, exception.Errors)); } }
public async Task <HttpResponseMessage> Post(ContentRepresentation content) { if (content == null) { return(Request.CreateResponse(HttpStatusCode.NotFound)); } if (!await AuthorizationService.AuthorizeAsync(ClaimsPrincipal, new ContentResourceAccess(content.ParentId), AuthorizationPolicies.ContentCreate)) { return(Request.CreateResponse(HttpStatusCode.Unauthorized)); } try { //we cannot continue here if the mandatory items are empty (i.e. name, etc...) if (!ModelState.IsValid) { throw ValidationException(ModelState, content, LinkTemplates.Content.Root); } var contentType = Services.ContentTypeService.GetContentType(content.ContentTypeAlias); if (contentType == null) { ModelState.AddModelError("content.contentTypeAlias", "No content type found with alias " + content.ContentTypeAlias); throw ValidationException(ModelState, content, LinkTemplates.Content.Root); } //create an item before persisting of the correct content type var created = Services.ContentService.CreateContent(content.Name, content.ParentId, content.ContentTypeAlias, ClaimsPrincipal.GetUserId() ?? 0); //Validate properties var validator = new ContentPropertyValidator <IContent>(ModelState, Services.DataTypeService); validator.ValidateItem(content, created); if (!ModelState.IsValid) { throw ValidationException(ModelState, content, LinkTemplates.Content.Root); } Mapper.Map(content, created); Services.ContentService.Save(created, ClaimsPrincipal.GetUserId() ?? 0); var msg = Request.CreateResponse(HttpStatusCode.Created, Mapper.Map <ContentRepresentation>(created)); msg.Headers.Add("location", VirtualPathUtility.ToAbsolute(LinkTemplates.Content.Self.CreateLink(new { id = created.Id }).Href)); return(msg); } catch (ModelValidationException exception) { return(Request.CreateResponse(HttpStatusCode.BadRequest, exception.Errors)); } }
public Task <HttpResponseMessage> Post(MemberRepresentation content) { if (content == null) { return(Task.FromResult(Request.CreateResponse(HttpStatusCode.NotFound))); } try { //we cannot continue here if the mandatory items are empty (i.e. name, etc...) if (!ModelState.IsValid) { throw ValidationException(ModelState, content, LinkTemplates.Members.Root); } var contentType = Services.MemberTypeService.Get(content.ContentTypeAlias); if (contentType == null) { ModelState.AddModelError("content.contentTypeAlias", "No member type found with alias " + content.ContentTypeAlias); throw ValidationException(ModelState, content, LinkTemplates.Members.Root); } //create an item before persisting of the correct content type var created = Services.MemberService.CreateMember(content.Email, content.Email, content.Name, content.ContentTypeAlias); //Validate properties var validator = new ContentPropertyValidator <IMember>(ModelState, Services.DataTypeService); validator.ValidateItem(content, created); if (!ModelState.IsValid) { throw ValidationException(ModelState, content, LinkTemplates.Members.Root); } Mapper.Map(content, created); Services.MemberService.Save(created); var msg = Request.CreateResponse(HttpStatusCode.Created, Mapper.Map <MemberRepresentation>(created)); AddLocationResponseHeader(msg, LinkTemplates.Members.Self.CreateLink(new { id = created.Id })); return(Task.FromResult(msg)); } catch (ModelValidationException exception) { return(Task.FromResult(Request.CreateResponse(HttpStatusCode.BadRequest, exception.Errors))); } }
private Task <HttpResponseMessage> PutInternal(Func <IMember> getMember, MemberRepresentation content) { if (content == null) { return(Task.FromResult(Request.CreateResponse(HttpStatusCode.NotFound))); } try { var found = getMember(); if (found == null) { return(Task.FromResult(Request.CreateResponse(HttpStatusCode.NotFound))); } //Validate properties var validator = new ContentPropertyValidator <IMember>(ModelState, Services.DataTypeService); validator.ValidateItem(content, found); if (!ModelState.IsValid) { throw ValidationException(ModelState, content, LinkTemplates.Members.Self, id: found.Id); } Mapper.Map(content, found); Services.MemberService.Save(found); var rep = Mapper.Map <MemberRepresentation>(found); return(Task.FromResult(Request.CreateResponse(HttpStatusCode.OK, rep))); } catch (ModelValidationException exception) { return(Task.FromResult(Request.CreateResponse(HttpStatusCode.BadRequest, exception.Errors))); } }
public async Task <HttpResponseMessage> Put(int id, ContentRepresentation content) { if (content == null) { return(Request.CreateResponse(HttpStatusCode.NotFound)); } if (!await AuthorizationService.AuthorizeAsync(ClaimsPrincipal, new ContentResourceAccess(id), AuthorizationPolicies.ContentUpdate)) { return(Request.CreateResponse(HttpStatusCode.Unauthorized)); } try { var found = Services.ContentService.GetById(id); if (found == null) { throw new HttpResponseException(HttpStatusCode.NotFound); } //Validate properties var validator = new ContentPropertyValidator <IContent>(ModelState, Services.DataTypeService); validator.ValidateItem(content, found); if (!ModelState.IsValid) { throw ValidationException(ModelState, content, LinkTemplates.Content.Self, id: id); } //lookup template if (content.TemplateId != Guid.Empty) { found.Template = Services.FileService.GetTemplate(content.TemplateId); if (found.Template == null) { ModelState.AddModelError("content.templateId", "No template found with id " + content.TemplateId); throw ValidationException(ModelState, content, LinkTemplates.Content.Root); } } else { found.Template = null; } Mapper.Map(content, found); if (!content.Published) { //if the flag is not published then we just save a draft Services.ContentService.Save(found, ClaimsPrincipal.GetUserId() ?? 0); } else { //publish it if the flag is set, if it's already published that's ok too var result = Services.ContentService.SaveAndPublishWithStatus(found, ClaimsPrincipal.GetUserId() ?? 0); if (!result.Success) { SetModelStateForPublishStatus(result.Result); throw ValidationException(ModelState, content, LinkTemplates.Content.Self, id: id); } } var rep = Mapper.Map <ContentRepresentation>(found); return(Request.CreateResponse(HttpStatusCode.OK, rep)); } catch (ModelValidationException exception) { return(Request.CreateResponse(HttpStatusCode.BadRequest, exception.Errors)); } }