CRUD App: Getting IDs to delete and update records - haskell

I am trying to create a very simple CRUD App and I am unsure what's the best way to update and delete records. I am able to successfully list all records and create a new record. Now I would now like to delete these records through the app and it is not immediately clear on how I should do it.
Let's say I have a Person table with just one field Name like so:
Person
name Text
I have a handler called PersonR that lists all the persons in the table and a form to input a new one. For handling delete and update, I thought I can create a hidden field called personId and then process the form through an InputForm but I couldn't get the ID out easily. I tried unKey personId but it still has PersistInt64 1 so I am presuming this is not the right way to go about it even if I manage to fish the ID out of there.
Another approach I could take is create a new handler (say ListPersons) just to list all persons and then change the Person handler to Person/#personId. I prefer to avoid this if possible and keep all actions related to Person in one place if possible.
Could you please let me know if I am thinking about this right and any suggestions for implementing a basic CRUD App functionality?
Update: I ended up creating a PersonPanel which will handle GET and POST. The Person Handler took care of DELETE and PUT. I ended up putting the 4 Handlers in the same Person handler file so it is not scattered around. Hope this helps others.
Thanks!

How about using toPathPiece in Web.PathPieces.PathPiece class to convert a key to Text, and putting it into a hidden field?
I guess yesod uses this class to convert a key to Text when it encodes the key into a type-safe URL, and vice versa.
Even though you can convert a key to Text in this way, a preferred way would be sending a DELETE request to Person/#personId. You can generate this URL using #{...} in your hamlet template.

Related

Orchard Content Parts that Don't Store Information in the Database

I'm trying to create a content part in Orchard that will perform a request to a web server and display the results of the request. The problem I'm running into is that my part requires no user input and thus I have left the part and part record classes empty. I have a Migrations.cs file that adds a description to the part and makes it attachable as well as creates a content item with the part attached to it. When I go to create a new instance of my content type it tries writing to the database and fails. How do you create a content part in orchard that doesn't try to save to the database? Thank you.
The actual error I receive is:
null id in Orchard.ContentManagement.Records.ContentTypeRecord
I'm pretty sure you don't need to create new table since there are many parts which don't have any in Orchard. Try to remove MyCustomPartRecord.cs and change MyCustomPart.cs
public class MyCustomPart : ContentPart<MyCustomPartRecord>
to
public class MyCustomPart : ContentPart
Then just add driver and view and you should be good without extra tables ... In theory :D
The answer to my problem is even when your part ins't actually saving anything in the database you still need to include a table for the part so it can store the id. I just added the following to my Migrations.cs file and it fixed the problem.
SchemaBuilder.CreateTable("MyCustomPartRecord",
table => table
.ContentPartRecord()
);

Retrieving app txt data! CoreData Swift

I have made a swift app that logs data into a sqlite file and it consists of 5 txt entry's and when someone enters data into them all and press's save, they will be saved into my sqlite file!
But this issue is when I want to retrieve data! I was following a tutorial on youtube on how to use coredata and save and retrieve inputed data into some text fields. But when he wrote the retrieve code, it only retreived the last known input! But I was wondering if there is anyway I could use only one of the textboxs info to then bring up all the rest of the info in all 5 text fields and Press Retrieve and it will bring it all up! Sort of like a password and then when correct it shows the data that was saved under that password!
Sorry this has been a bit long winded and before you ask, here is the code I used http://pastebin.com/BEq496tY
Thanks,
George Barlow
First, rename your entity from ID to something else, and the attribute id to something else. "ID" is used for so many things, you are likely to run into a problem. I will call your entity Item and the id idNumber.
Second, when saving your object, you take the stuff from the text fields and put it into the appropriate place, e.g.
item.txt1 = textField1.text
item.txt2 = textField2.text
// etc.
Similarly, when retrieving, you should populate all text fields.
textField1.text = item.txt1
textField2.text = item.txt2
// etc.
To have some kind of search functionality you would have to first find out which field is filled with data and then search for that with a predicate, like
NSPredicate(format: "txt1 = %#", textField1.txt)
To create new items you would do something like
let item = NSEntityDescription. insertNewObjectForEntityForName("Item",
inManagedObjectContext:self.managedObjectContext)
// configure item
self.managedObjectContext.save(nil)
That's all there is to it.

Reusing a custom ContentPart in the same ContentType

I'm trying to figure out the best way to handle a requirement I have for an Orchard module I'm building.
I have a ContentPart that has a few fields. One field is a ContentPicker that allows for multiple items to be associated to the part. The rest are descriptive information.
The issue I have is that I actually need to be able to include more than one of this ContentPart into a ContentType. I need to create a ContentType that has exactly 3 of this part.
Should I be making this into a field instead of a part? Is it even possible to have a ContentField that has other fields in it?
Or, should I somehow use all the same models and data structures, but somehow define it as 3 distinct parts?
Just wondering what the best practice to do something like this would be.
You can only have one part of each kind on a given type. You can't have fields that have other fields in it (instead - take an existing field and extend it with custom stuff).
As I understand, the actual problem is "how to make groups of fields with some metadata for each group", right? If so, there are a few approaches to solve the problem:
Create a custom field based on Content Picker (basically - take existing Content Picker and extend it with your metadata) and use this without the need for a separate part
Create one part to hold only the metadata for each field attached to it and attach 1 or more fields to it
Create 3 distinct parts. Parts should be thought of as extensions that add some unique features to an item. If you think it's logically ok to have 3 parts then go for it.

How to create and fetch relational records in core data

Total newbie question now... Suffice to say, I have searched for a completely noddy explanation but have not found anything 'dumb' enough. The problem is...
I have created a core data stack in which I have a entity called 'Client' and an entity called 'Car'. It is a one-to-many relationship.
So far i have successfully created and fetched the client list using code from apple's tutorial. Once I select a client, I then push a new tableViewController which should list the Cars for that chosen client.
First question...
I am used to sql style database programming where if I wanted to add a car to a client, I would simply add a 'ClientID' tag to the 'Car' record thereby providing the relationship to a specific client. How do I do the equivalent in core data? My understanding from my reading is adding attributes to point to other entities isnt necessary - core data maintains this relationship for you without needing additional attributes in the entities.
Second question...
As and when I have created a 'car' entity and successfully linked it to a 'Client'. How to I create a fetch which will retrieve just THAT client's cars. I could alter the code from apple to fetch ALL cars but I don't know how to fetch cars associated with a given client. From my reading, I think I need to use predicates, but apples predicate documentation stands alone and does not give clear guidance on how to use it with core data
I realise how noddy this is, but I cant find an idiots guide anywhere...
Any help/code exmaples much appreciated.
OK, I have answered my own question. For those who have found my question and would like to know the answer, it is extremely simple...
Firstly, to create a 'Car' and associate it with a 'Client'. Firstly create a 'Car' as you normally would and simply add this line of code...
newCar.client = client;
This sets the 'client' relationship on the 'Car' record to the client in question.
Secondly, I had thought that if you had a client and needed to find their cars, you would need a new fetch. But not so! Simply use the following lines of code...
NSSet *cars = client.cars;
[self setCarsArray:[cars allObjects]];
The first line uses "client.cars" o follow the object graph to determine the cars this client has and populates them in an NSSet. The second line then populates a NSArray which is declared in the current viewcontroller which can be used to for display purposes.
Sorted!!

Restore one fetched entity out of many -- Core Data

This question covncerns my lack of understanding of how to use the core data undo manager and how to restore a NSManagedObject to its state before editing was done.
I am just learning my way around Core Data. I have my NSManagedObject classes set up with their dynamic accessors. I perform a fetch that returns several NSManagedObject entity results. Content from each of these entity results (first name, last name) get put into a table view, and then the user picks one out of the table for detailed view and then editing.
The detail view controller receives a pointer to the selected NSManagedObject entity. As the user edits the fields, the corresponding property value in the NSManagedObject entity is updated. This seemed like the cleanest way to manage these changes.
Now, rather than committing the changes using save, I want to provide a cancel-editing feature that rolls back to what is in the data base for that entity. I really only want to restore the one entity and not perform the entire refetch.
I tried rollback and I tried NSUndoManager (with beginUndoGrouping and endUndoGrouping), and that is not working. I don't think I understand what rollback is really supposed to do.
But in any case, I still want to restore the property values in just that single entity (taking the lazy approach to only fetch what is needed, which is the one entity) so that my detail view controller can refill its view with the correct information. Right now it is using the NSManagedObject entity values, which contain the edited values, which were cancelled.
I suppose I could just start the edit process by creating a copy of the NSManagedObject. If the cancel-editing button is pressed, I could copy it back into the original. (I might even be able to just replace the original with the copy by moving the pointer. But since the pointer has actually been passed through several objects, I'm not sure how to manage the retain number on the copy.)
Does anyone have any other suggestions?
Thanks
Using rollback should accomplish what you want and I'm not sure what it doesn't. It is probably an implementation detail error.
You can find the specific managed object/s that were updated but not yet saved by calling the context's updatedObjects.

Resources