public async Task <DocumentRead> Create(DocumentEdit model) { if (model == null) { throw new ArgumentNullException("model"); } var userId = currentUserProvider.GetUserId(); var document = new Document().Merge(model); document.UserId = userId; document.CreatedAt = document.UpdatedAt = Clock.UtcNow(); dataContext.Documents.Add(document); await dataContext.SaveChangesAsync(); return(new DocumentRead { Id = document.Id, Title = document.Title, Content = document.Content, Owned = true, Shared = false, Editable = true, CreatedAt = document.CreatedAt, UpdatedAt = document.UpdatedAt }); }
/// <summary> /// Initializes a new instance of the <see cref="DocumentEditViewModel" /> class. /// Constructor for initializing DocumentEditViewModel. /// </summary> /// <param name="model">The DocumentEdit model.</param> /// <param name="parentViewModel">Parent model.</param> /// <param name="satisfyImports">if set to <c>true</c> [satisfy imports].</param> public DocumentEditViewModel(DocumentEdit model, DocumentListProcessViewSectionStepViewModel parentViewModel, bool satisfyImports = true) { FieldTypeFilters = new Collection<ColumnTypes>() { ColumnTypes.MultiReference, ColumnTypes.Reference }; Parent = parentViewModel; LoadProcesses(); SetModel(model); DetailField = Model.DetailObject; if (Model.HeaderObject != string.Empty && !string.IsNullOrEmpty(Model.HeaderFields)) { var field = AvailableFields.FirstOrDefault(f => f.SystemName == Model.HeaderObject); if (field != null) { var requiredStep = (CrossRefRequiredStepEdit) field.StepList.FirstOrDefault(s => s is CrossRefRequiredStepEdit); if (requiredStep != null) { FieldList.BeginGetCrossReferenceSubfields( requiredStep.Process.SystemName, this.Model.HeaderObject, (sender, result) => { if (result.Error == null && string.IsNullOrEmpty(Model.HeaderDisplayFields)) { var derivedFileds = new ObservableCollection<FieldInfo>(result.Object); foreach (var str in this.Model.HeaderFields.Split(',')) { if (!string.IsNullOrEmpty(str)) { var lfieldViewModel = derivedFileds.FirstOrDefault(f => f.SystemName == str); if (string.IsNullOrEmpty(Model.HeaderDisplayFields)) Model.HeaderDisplayFields = lfieldViewModel.Name; else Model.HeaderDisplayFields += ", " + lfieldViewModel.Name; } } } }); } } } if (satisfyImports) Ioc.SatisfyImportsOnce(this); }
public async Task <HttpResponseMessage> Put(int id, DocumentEdit model) { if (!ModelState.IsValid) { return(Request.CreateErrorResponse( HttpStatusCode.BadRequest, ModelState)); } var result = await service.Update( id, model, () => { throw new HttpResponseException( HttpStatusCode.NotFound); }); return(Request.CreateResponse(HttpStatusCode.OK, result)); }
public async Task<HttpResponseMessage> Post(DocumentEdit model) { if (!ModelState.IsValid) { return Request.CreateErrorResponse( HttpStatusCode.BadRequest, ModelState); } var result = await service.Create(model); HttpResponseMessage response = null; try { response = Request.CreateResponse( HttpStatusCode.Created, result); var location = Url.Link("DefaultApi", new { id = result.Id }); if (location != null) { response.Headers.Location = new Uri(location); } return response; } catch { if (response != null) { response.Dispose(); } throw; } }
public async Task <HttpResponseMessage> Post(DocumentEdit model) { if (!ModelState.IsValid) { return(Request.CreateErrorResponse( HttpStatusCode.BadRequest, ModelState)); } var result = await service.Create(model); HttpResponseMessage response = null; try { response = Request.CreateResponse( HttpStatusCode.Created, result); var location = Url.Link("DefaultApi", new { id = result.Id }); if (location != null) { response.Headers.Location = new Uri(location); } return(response); } catch { if (response != null) { response.Dispose(); } throw; } }
public async Task <DocumentRead> Update(int id, DocumentEdit model, Action notFound) { if (model == null) { throw new ArgumentNullException("model"); } if (notFound == null) { throw new ArgumentNullException("notFound"); } var userId = currentUserProvider.GetUserId(); var ownedDocumentsQuery = dataContext.Documents .Where(d => d.Id == id && d.UserId == userId) .Select(d => new { document = d, shared = d.Shares.Any(), owned = true }); var sharedDocumentsQuery = dataContext.Documents .Where(d => d.Id == id && d.Shares.Any(s => s.UserId == userId && s.CanEdit)) .Select(d => new { document = d, shared = true, owned = false }); var info = await ownedDocumentsQuery .Concat(sharedDocumentsQuery) .FirstOrDefaultAsync(); if (info == null) { notFound(); return(null); } info.document.Merge(model); info.document.UpdatedAt = Clock.UtcNow(); await dataContext.SaveChangesAsync(); return(new DocumentRead { Id = info.document.Id, Title = info.document.Title, Content = info.document.Content, Owned = info.owned, Shared = info.shared, Editable = true, CreatedAt = info.document.CreatedAt, UpdatedAt = info.document.UpdatedAt }); }
/// <summary> /// Sets the model. /// </summary> /// <param name="model">The model.</param> private void SetModel(DocumentEdit model) { Model = model; }
public async Task<DocumentRead> Update(int id, DocumentEdit model, Action notFound) { if (model == null) { throw new ArgumentNullException("model"); } if (notFound == null) { throw new ArgumentNullException("notFound"); } var userId = currentUserProvider.GetUserId(); var ownedDocumentsQuery = dataContext.Documents .Where(d => d.Id == id && d.UserId == userId) .Select(d => new { document = d, shared = d.Shares.Any(), owned = true }); var sharedDocumentsQuery = dataContext.Documents .Where(d => d.Id == id && d.Shares.Any(s => s.UserId == userId && s.CanEdit)) .Select(d => new { document = d, shared = true, owned = false }); var info = await ownedDocumentsQuery .Concat(sharedDocumentsQuery) .FirstOrDefaultAsync(); if (info == null) { notFound(); return null; } info.document.Merge(model); info.document.UpdatedAt = Clock.UtcNow(); await dataContext.SaveChangesAsync(); return new DocumentRead { Id = info.document.Id, Title = info.document.Title, Content = info.document.Content, Owned = info.owned, Shared = info.shared, Editable = true, CreatedAt = info.document.CreatedAt, UpdatedAt = info.document.UpdatedAt }; }
public async Task<DocumentRead> Create(DocumentEdit model) { if (model == null) { throw new ArgumentNullException("model"); } var userId = currentUserProvider.GetUserId(); var document = new Document().Merge(model); document.UserId = userId; document.CreatedAt = document.UpdatedAt = Clock.UtcNow(); dataContext.Documents.Add(document); await dataContext.SaveChangesAsync(); return new DocumentRead { Id = document.Id, Title = document.Title, Content = document.Content, Owned = true, Shared = false, Editable = true, CreatedAt = document.CreatedAt, UpdatedAt = document.UpdatedAt }; }
public async Task<HttpResponseMessage> Put(int id, DocumentEdit model) { if (!ModelState.IsValid) { return Request.CreateErrorResponse( HttpStatusCode.BadRequest, ModelState); } var result = await service.Update( id, model, () => { throw new HttpResponseException( HttpStatusCode.NotFound); }); return Request.CreateResponse(HttpStatusCode.OK, result); }