/// <summary> /// Usergroup module /// </summary> public UsergroupModule() { // get this.Get( "/Usergroup", x => { var entity = "Usergroup"; var type = ApiHelper.GetEntityTypeFromName(@"Redshift.Seed.Model", entity, "Redshift.Seed"); List <Attribute> attributes; var typeErrors = ApiHelper.GetTypeErrors(this.Negotiate, entity, type, out attributes, this.Context); if (typeErrors != null) { return(typeErrors); } List <Usergroup> resp; QueryParameterContainer queryParams; var count = -1L; try { queryParams = this.ProcessQueryParameters(this.Request, type); } catch (Exception) { return(ApiHelper.ConstructFailResponse( this.Negotiate, entity, "The query parameters are badly formatted and cannot be parsed.", this.Context, HttpStatusCode.BadRequest)); } try { if (queryParams.IsFiltered && !queryParams.IsPaginated) { resp = Usergroup.Where(queryParams.FilterList, true, null, null, queryParams.OrderProperty, queryParams.IsDescending); if (queryParams.IsCountExpected) { // all entities are already queried and in memory so simple count is fine count = resp.Count; } } else if (queryParams.IsPaginated) { // when paginating, always return count queryParams.IsCountExpected = true; resp = Usergroup.Where(queryParams.FilterList, true, queryParams.Limit, queryParams.Offset, queryParams.OrderProperty, queryParams.IsDescending); // paginated response provide count always and the count is of total filtered records count = Usergroup.CountWhere(queryParams.FilterList); } else { resp = Usergroup.Where(queryParams.FilterList, true, null, null, null, true); if (queryParams.IsCountExpected) { // all entities are already queried and in memory so simple count is fine count = resp.Count; } } } catch (Exception e) { return(ApiHelper.ConstructErrorResponse(this.Negotiate, e, this.Context)); } var response = new ResponseContainer(); response.AddToResponse(resp); // add count to the response if (queryParams.IsCountExpected) { response.Add("count", new List <object> { count }); } return(ApiHelper.ConstructSuccessResponse(this.Negotiate, response, this.Context)); }); this.Get( "/Usergroup/{uuid:guid}", x => { // parse id var uuid = ApiHelper.GetIdFromString(x.uuid.ToString()); if (uuid == null) { return(ApiHelper.ConstructFailResponse( this.Negotiate, "uuid", string.Format("The requested uuid {0} cannot be parsed.", x.uuid), this.Context, HttpStatusCode.BadRequest)); } // parse entity var entity = "Usergroup"; var type = ApiHelper.GetEntityTypeFromName(@"Redshift.Seed.Model", entity, "Redshift.Seed"); List <Attribute> attributes; var typeErrors = ApiHelper.GetTypeErrors(this.Negotiate, entity, type, out attributes, this.Context); if (typeErrors != null) { return(typeErrors); } // acquire the response Usergroup resp; try { resp = Usergroup.Find(uuid); } catch (Exception e) { return(ApiHelper.ConstructErrorResponse(this.Negotiate, e, this.Context)); } if (resp == null) { return(ApiHelper.ConstructFailResponse( this.Negotiate, "uuid", string.Format("The requested {1} with id {0} does not exist.", uuid, entity), this.Context)); } var response = new ResponseContainer(); response.AddToResponse(resp); return(ApiHelper.ConstructSuccessResponse(this.Negotiate, response, this.Context)); }); this.Post( "/Usergroup", x => { var postBody = RequestStream.FromStream(this.Request.Body).AsString(); var entity = "Usergroup"; var type = ApiHelper.GetEntityTypeFromName(@"Redshift.Seed.Model", entity, "Redshift.Seed"); List <Attribute> attributes; var typeErrors = ApiHelper.GetTypeErrors( this.Negotiate, entity, type, out attributes, this.Context); if (typeErrors != null) { return(typeErrors); } // demoinstrates a way to generalize return(this.PerformSimplePost(type, postBody)); }); this.Put( "/Usergroup/{uuid:guid}", x => { var uuid = ApiHelper.GetIdFromString(x.uuid.ToString()); var putBody = RequestStream.FromStream(Request.Body).AsString(); var entity = "Usergroup"; var type = ApiHelper.GetEntityTypeFromName(@"Redshift.Seed.Model", entity, "Redshift.Seed"); var typeErrors = ApiHelper.GetTypeErrors( this.Negotiate, entity, type, out var attributes, this.Context); if (typeErrors != null) { return(typeErrors); } IEntityObject savedResp; try { // acquire the response Usergroup resp; try { resp = Usergroup.Find(uuid); if (resp != null) { try { JsonConvert.PopulateObject( putBody, resp); // make sure the uuid is set correctly/not being changed. resp.Uuid = uuid; } catch (Exception e) { return(ApiHelper.ConstructFailResponse(this.Negotiate, "update", "The request was not properly formatted and could not be used to update the object!", this.Context, HttpStatusCode.BadRequest, e)); } } else { // if there is an object of this type in the database then this POST is invalid return(ApiHelper.ConstructFailResponse( this.Negotiate, type.Name, "An object with this Uuid does not exist and cannot be updated!", this.Context, HttpStatusCode.NotFound)); } } catch (Exception e) { return(ApiHelper.ConstructErrorResponse(this.Negotiate, e, this.Context)); } var transaction = DatabaseSession.Instance.CreateTransaction(); savedResp = resp.Save(transaction: transaction); DatabaseSession.Instance.CommitTransaction(transaction); } catch (Exception e) { return(ApiHelper.ConstructErrorResponse(this.Negotiate, e, this.Context)); } var container = new ResponseContainer(); container.AddToResponse(savedResp); return(ApiHelper.ConstructSuccessResponse(this.Negotiate, container, this.Context)); });
public UsergroupsModule() : base("/admin") { this.RequiresAuthentication(); this.RequiresClaims(new[] { "Users" }); this.Get["/usergroups"] = x => { this.Model.Usergroups = new UsergroupsModel(); return(this.View["admin/Usergroups", this.Model]); }; this.Get["/usergroups/{id:guid}"] = x => { var usergroup = Usergroup.Find(Guid.Parse(x.id)); if (usergroup == null) { return(HttpStatusCode.NotFound); } this.Model.Usergroup = usergroup; return(this.View["admin/usergroup", this.Model]); }; this.Get["/usergroups/create"] = x => { this.Model.Usergroup = new Usergroup(); this.Model.Claims = Claim.All(); return(this.View["admin/UsergroupEdit", this.Model]); }; this.Post["/usergroups/create"] = x => { // do the save var name = (string)this.Request.Form.Name; var claims = (string)this.Request.Form.Claims; var master = (MasterModel)this.Model.MasterModel; master.Errored = false; master.ErrorsList.Clear(); var newUsergroup = new Usergroup() { Id = Guid.NewGuid(), Name = name }; var allUsergroups = Usergroup.All(); if (string.IsNullOrWhiteSpace(newUsergroup.Name)) { master.ErrorsList.Add("The name must not be empty."); } if (allUsergroups.Any(u => u.Name.Equals(newUsergroup.Name))) { master.ErrorsList.Add("The provided name is already taken."); } // set the claims newUsergroup.Claims = claims.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries).ToList().Select(Guid.Parse).ToList(); // save if (master.ErrorsList.Any()) { master.Errored = true; this.Model.Usergroup = newUsergroup; this.Model.Claims = Claim.All(); var u = this.BindTo(newUsergroup); return(this.View["admin/UsergroupEdit", this.Model]); } newUsergroup.Save(); // redirect to the list return(this.Response.AsRedirect("/admin/usergroups")); }; this.Get["/usergroups/{id:guid}/edit"] = x => { var usergroup = Usergroup.Find(Guid.Parse(x.id)); if (usergroup == null) { return(HttpStatusCode.NotFound); } this.Model.Usergroup = usergroup; this.Model.Claims = Claim.All(); return(this.View["admin/UsergroupEdit", this.Model]); }; this.Post["/usergroups/{id:guid}/update"] = x => { // do the save var name = (string)this.Request.Form.Name; var claims = (string)this.Request.Form.Claims; var master = (MasterModel)this.Model.MasterModel; master.Errored = false; master.ErrorsList.Clear(); var oldUsergroup = Usergroup.Find((Guid)x.Id); var allUsergroups = Usergroup.All(); if (string.IsNullOrWhiteSpace(name)) { master.ErrorsList.Add("The name must not be empty."); } if (allUsergroups.Any(u => u.Name.Equals(name) && !u.Name.Equals(oldUsergroup.Name))) { master.ErrorsList.Add("The provided name is already taken."); } oldUsergroup.Name = name; oldUsergroup.Claims = claims.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries).ToList().Select(Guid.Parse).ToList(); // save if (master.ErrorsList.Any()) { master.Errored = true; this.Model.Usergroup = oldUsergroup; this.Model.Usergroups = Claim.All(); var u = this.BindTo(oldUsergroup); return(this.View["admin/UsergroupEdit", this.Model]); } oldUsergroup.Save(); // redirect to the list return(this.Response.AsRedirect("/admin/usergroups")); }; this.Post["/usergroups/{id:guid}/remove"] = x => { var usergroup = Usergroup.Find((Guid)x.id); if (usergroup == null) { return(HttpStatusCode.NotFound); } // remove the user try { usergroup.Delete(); } catch (InvalidDataException) { var master = (MasterModel)this.Model.MasterModel; master.Errored = true; master.ErrorsList.Add("You cannot delete a usergroup that has members assigned."); this.Model.Usergroups = new UsergroupsModel(); return(this.View["admin/Usergroups", this.Model]); } return(this.Response.AsRedirect("/admin/usergroups")); }; }