Monday 27 April 2020

Simplest CRUD - Ember web service (Web API) tutorial

Ember adapters
https://guides.emberjs.com/v3.15.0/models/customizing-adapters/

Serializer
https://guides.emberjs.com/v3.15.0/models/customizing-serializers/

The tutorial
https://medium.com/ember-ish/the-simplest-possible-ember-data-crud-16eacee33ae6

I am following the above tutorial...(27/4/20)

API + db all working well.


CORS issue was also fixed by using middleware.

Enable CORS in Lumen




But I still had issue in GET operation


The lady author says,

"If your POST request failed and this surprises you, a few different things could be wrong:"

I guess the issue is the json format.
https://guides.emberjs.com/v3.15.0/models/customizing-serializers/

My guess was right. Ember serializer default is JsonWAPISerializer. You have to change it to JsonSerializer to work with Lumen's default json formats without 'data' or 'attribute' name conventions.



Now /api/boardgames/1 is working.





But insert is still not working. Why?


Amazingly, the issue was in database. The sample Lumen API server code (not this tutorial) I downloaded from below required updated_at, created_at in each table. After updating the database, POST call to create new boardgame worked fine!!

Lumen tutorial:
https://auth0.com/blog/developing-restful-apis-with-lumen/




Now insert is working




16/5/20. At one stage, insert stopped working again, this time with CORS issue again.  My table had two date columns, middle ware was there (GET was still working). I found suspecious error in Lumen console. It seems my PHP in LInux(7.1) is not good enough for SImpony (lumen insert operation). The same code was working on my mac (php 7.2) fine?





 Why Delete not working?


I know it is an API error while returning the result. because when I refreshed the page, the record was deleted already. And it didn't show any error while debugging. Debugging was a bit tricky to find where to see.


Web API returns string and it was not a correct JSON format. You have to implement Ember serializer.

public function delete($id)
{
BoardGame::findOrFail($id)->delete();
return response('Deleted Successfully', 200);
}

As soon as I change Web API delete as below, Ember site started working.

https://stackoverflow.com/questions/28387693/ember-promise-destroyrecord-then-failing-on-successful-request



public function delete($id)
{
BoardGame::findOrFail($id)->delete();
//return response('Deleted Successfully', 200);
//below is working as well. but you don't seem to get $newContent.
// $newContent = [
// 'data' => 'deleted'
// ];
// return response()->json($newContent, 200);
//https://stackoverflow.com/questions/28387693/ember-promise-destroyrecord-then-failing-on-successful-request
return response('Deleted Successfully', 204);
}



Why update not working?



I found that Ember translates updates as 'PATCH' operation.

  • I tried PATCH in postman it didn't work. 
  • Also PUT operation had to use www-encode option (?). 
  • There must be something special about PUT operation. 
  • Google search 'ember use PUT instead of PATCH'. Then the below link, the 1st solution worked for me not the second. 
  • So you have to convert PATCH to PUT operation with below code
  • app/adapters/application.js


https://github.com/emberjs/data/issues/3870

export default DS.JSONAPIAdapter.extend({
host: 'http://localhost:8000/api',
updateRecord: function (store, type, snapshot) {
var data = {};
var serializer = store.serializerFor(type.modelName);
serializer.serializeIntoHash(data, type, snapshot, { includeId: true });
var id = snapshot.id;
var url = this.buildURL(type.modelName, id, snapshot, 'updateRecord');
return this.ajax(url, 'PUT', { data: data });
}
})

Summary

Make sure you put model() hook in the router not controller js.

Here is the completed Ember web API, the simplest one:
https://github.com/oneman93/ember-simplest-crud


I like to share my web service API as well, but because I did update in there for my personal stuff, I cannot share it. However, the Lumen tutorial above will be enough to start up a web service server.

-----------

Webservice other urls

youtube about ember data
https://www.youtube.com/watch?v=ljLxZw-XStw

mirage tutorial
https://guides.emberjs.com/v3.4.0/tutorial/ember-data/


Friday 3 April 2020

ember-modal-dialog

https://github.com/yapplabs/ember-modal-dialog#live-demo-and-test-examples

Followed the link.
1. Installed the ember-modal-dialog inside ember project folder.
2. Restarted ember - ember s
3. Page refresh. hola!

Thursday 2 April 2020

git tag

Create git tag in old commit

I like to add git tag to one of my old commits, say, e9c4a35.

% git tag                
% git tag -a v1.0 e9c4a35
or 
% git tag -a v1.0 e9c4a35 -m 'multi page complete'

% git tag                
v1.0

Then you push the tag to remote server

% git push origin v1.0
Enumerating objects: 1, done.

Now your github site will have this tag.
See more in
or
visual github

How to create remote git tag without local tag with hash code, I don't know.

<annotated vs non-annotated tag>

When you use git tag <tagname>, Git will create a tag at the current revision but will not prompt you for an annotation. It will be tagged without a message (this is a lightweight tag).

When you use git tag -a <tagname>, Git will prompt you for an annotation unless you have also used the -m flag to provide a message.


https://stackoverflow.com/questions/11514075/what-is-the-difference-between-an-annotated-and-unannotated-tag



<git describe>

The command finds the most recent tag that is reachable from a commit. If the tag points to the commit, then only the tag is shown. Otherwise, it suffixes the tag name with the number of additional commits on top of the tagged object and the abbreviated object name of the most recent commit.

By default (without --all or --tags) git describe only shows annotated tags

https://git-scm.com/docs/git-describe

<git describe --tags>

It shows any tag found in refs/tags namespace. This option enables matching a lightweight (non-annotated) tag.

If the environment is on a branch that is 14 commits ahead of the last tag, this will show, eg, v0.0.1-14-gXXX, where XXX is SHA hash value.

https://git-scm.com/docs/git-describe
https://www.soliantconsulting.com/blog/versioning-with-git-describe/


<git push with tag>

By default, the git push command doesn’t transfer tags to remote servers. You will have to explicitly push tags to a shared server after you have created them. This process is just like sharing remote branches — you can run git push origin <tagname>.

https://git-scm.com/book/en/v2/Git-Basics-Tagging
https://stackoverflow.com/questions/5195859/how-do-you-push-a-tag-to-a-remote-repository-using-git




<problem with git describe --tags>
 git push -f origin CE-7757

I pushed without tag, but it still prints tag information.


#!/bin/bash -eo pipefail
echo "$(git describe --tags)"
echo 'cur_version=$(git describe --tags)' >> $BASH_ENV
v0.0.1-7-g6e4a85b4f




<CIRCLE_TAG is empty>

https://discuss.circleci.com/t/circle-tag-is-empty/18919?utm_medium=SEM&utm_source=gnb&utm_campaign=SEM-gb-DSA-Eng-ni&utm_content=&utm_term=dynamicSearch-&gclid=EAIaIQobChMI0f2W_OnG6AIVRw4rCh2lhAxYEAAYASAAEgLsOPD_BwE

https://support.circleci.com/hc/en-us/articles/360020342494-The-built-in-environment-variable-CIRCLE-TAG-is-not-present


<git push - NO BRANCH BUT TAG???>


git push without parameter
https://stackoverflow.com/questions/948354/default-behavior-of-git-push-without-a-branch-specified

<example>

$ git commit -a -m 'test v0.0.0'
[CE-7757 334779fbc] test v0.0.0
 1 file changed, 2 insertions(+), 2 deletions(-)
$ git tag v0.0.0
$ git describe --tags
v0.0.0

$ git commit -a -m 'test v0.0.1'
[CE-7757 d077f2b85] test v0.0.1
 1 file changed, 1 insertion(+), 1 deletion(-)
$ git tag v0.0.1
$ git describe --tags
v0.0.1

$ git push origin CE-7757 v0.0.0
Counting objects: 8, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (6/6), done.
Writing objects: 100% (8/8), 763 bytes | 763.00 KiB/s, done.
Total 8 (delta 4), reused 0 (delta 0)
remote: Resolving deltas: 100% (4/4), completed with 2 local objects.
To github.com:xxx.git
   97635f5ee..d077f2b85  CE-7757 -> CE-7757
 * [new tag]             v0.0.0 -> v0.0.0





After the push, search by branch and tag will give you different point of HEAD.






Also, note the push result. Circleci does not returns v0.0.0 but v0.0.0-1-gxxx because the 'git describe --tags' will return 'the most recent tag reachable from the commit'. Considering entire commits, current commit was 1 time after the tag commit. The server github does not have any idea about v0.0.1 yet, but knows that the last commit (by branch) is 1 time after the tag's commit. So...the same command 'git describe --tags' can show different result when you run it on local machine and remote circle ci environment (if you use different tag on push).