API Upload files with metadata - laravel

I'm looking for some guidance here.
Scenario:
I have a Post model with a polymorphic relation to a Comment model. Whenever I want to create a new comment for a given post I have the following endpoint:
$router->post('/posts/{post}/comments', 'PostsCommentsController#store');
So far so good. Now I want to add an Attachment model which will also be a polymorphic relation since I may need to add attachments to more things other than comments (ex: messages, etc).
My first idea was doing something in the lines of:
$router->post('/posts/{post}/comments/attachments', 'PostsCommentsAttachmentsController#store');
So the comment will belong to a post and will have an attachment.
This feels a bit "dirty" to me (especially the controller name) and the need of having 3 nested resources (maybe I'm just thinking too much).
Hope I was clear enough explaining my problem :)
Have anyone faced something like this before? How did you guys solve it?
Other approaches? Am I thinking completely wrong?
Open to ideas and suggestions :D
Thank you all.

I'd prefer Single Responsibility for each Controllers or route. So it's pretty clear what their actually do and handle. Let me give you example:
- Post
/posts -> list all post
/posts/{id} -> get specific post
/posts/{id}/comments -> get comments of the post
- Comment
/comments/{id} -> get specific comment
/comments/{id}/attacments -> get attacments of a comment
- Attachment
/attachment/{id} -> get specific attachment
For Controller name, just keep it simple. Just usePostController, CommentController and AttachmentController. It's quite clear I think.

After more researching, I actually realized that what I wanted to do was basically a file upload with metadata.
Came across this excellent post: HTTP/REST API File Uploads that explains it
PS: Thank you Dharma for the help and time.

Related

GET a slide, edit it, and POST it again with minor changes

I was hoping someone could help me with something i've been stuck on. I'm not even sure if it's possible to do.
So i basically have a huge Json file which includes all objects used for a certain slide Specifically i used this GET command:
GET https://slides.googleapis.com/v1/presentations/{presentationId}
I then got a huge 200.000 line Json response which has alot of stuff like colors for each thing, position of every element on each slide ect. I save this as a JSON file on my pc. I only need it once as a form of template.
Then my golang code dynamically edits some of its values (after converting it to structs ofc).
Now i want to POST it back up. It has a new name now, new ID, but 99,9% of the values are the same.
Is it possible to do this?
And sorry in advance. I know people here tend to get mad at "stupid questions" or if i forget to add something, but i'm new here, and i hope I can get some help. Been stuck for a long while.
Yes, you can. There is a Go client library that allows you to do this if you're not set on using a REST API. If you are set on using a REST API, you should be able to post this endpoint:
POST https://slides.googleapis.com/v1/presentations/{presentationId}:batchUpdate
Side note, the Google documentation (imo) is fantastic, a little googling goes a long ways :)

How to get the post author data to display it in an html box?

I'm not very used to working with Wix and I realized that it has a certain limit regarding the Blog layout. I would like to add the author of each post next to the post, but it would be too much work to do post by post.
So I created an HTML box to get the image and name of the author of the post. But I know I'm not doing it right. See:
Looking in the documentation the most I found was the function getPost() which returns various information from the post page, but 'author' or 'writer' is not among them.
My question is: is there any way to get the author data to display it in this box? Or, is there a simpler way to get to this desired layout?
Author information doesn't necessarily need to be inside an HTML box, if there's another way to do that, that's fine too!!
As seen on https://www.wix.com/velo/example/custom-post-page
You can get the ID of the post - which you already demonstrated you know how to do via getPost() then use that ID to retrieve the extra post data - which includes the currentPost.author entity that you are looking for. You can also see on the same page how this, and other data from that extra post data, can be brought into the page:
function assignPostDataToUIElements() {
$w("#authorName").text = `Written by: ${currentPost.author}`
}
User #Arnon De Paula definitely pointed us in the right direction with their link and should receive the bounty if this ends up being the answer you were looking for.

Aliasing ID parameter in URL as string in ASP.NET MVC 4

What i'm trying to do is to rewrite URLs to make them more SEO friendly but i still want to pass a parameter as an int ID.
For example, a URL pointing to a news article might look like this:
"www.domain.com/category-id/article-id" or "domain.com/5/3"
What i want to do is to rewrite the URL everywhere so that the title of the category and the title of the article are written into the URL so it becomes f.x. "domain.com/politics/some-title" but i still want to pass the ID of the article as an argument to the controller action. This is less important for the category but it's something i want to do with the article-id since it's unique but the title might not be.
I have checked out Attribute Routing and looked through some Routing guides and questions but haven't found anything that lets me implement this functionality. I've just started using ASP.NET MVC so i haven't been able to look into anything too advanced.
Thanks in advance.
I would advice to make the article title unique and from the controller action you have to get the article based on the title.
I see you are trying to group the articles based on category. When I initially created my blog I thought the same-thing but soon realized it's not a flexible approach because of couple of reasons.
Say you wrote one article with name some-title and dropped it under a category say politics and so the url will be domain.com/politics/some-title but at a later point of time you thought to move the article to another category say 'international-politics' therefore your url now has to be changed to domain.com/international-politics/some-title and you break the old url and whoever has bookmarked that link will now receive 404. A better way would be organize the urls based on the posted date and that's not going to change something like http://domain.com/archive/yyyy/mm/dd/unique_title
Sometimes you want to label an article with more than one category and at that time a tag based approach will become a better choice compared to category based approach.
Quick and dirty solutions:
1) domain.com/categoryName/articleID/articleName/
2) domain.com/date/categoryName/articleName (date should help make articleName unique)
3) domain.com/categoryName/articleName?id=xxx
Nothing fancy, but those approaches will work.

How to denied all requests except some in Magento

The question is not enough clear, so I will explain more.
My client has a requirement in mobile1 about the users all SHOULD not visit all request2
For example:
The users SHOULD: only visit some in below list:
mydomain.com/catalog/product/view
mydomain.com/cusotmer/account/any_action
mydomain.com/onepage/checkout/any_action
So, I am having a solution is at the method: Mage_Core_Controller_Varien_Action::preDispatch()
I will get the request variable to check what is moulde, controller, action. If it is valid in list, if not I will redirect to an accessed denied page.
I know that is bad, so I am looking for other solutions.
1: We've already designed a theme for mobile, just want to give for user some pages are necessary.
2: I mean to the router format: module/ controller/ action.
UPDATE:
Thanks all guys so much. Now, I understand what I need to do. I don't know how to close my question. So please help me if you can. I don't want to be a member who never accept the answer.
This is not good question which could be easy to answer, but very happy for help from #azakolyukin and #elgabbu
So, I think #azakolyukin's comment is correct answer.

post-redirect-get with notification about update

We usually follow the convention of doing a redirect after every post, which is ideally very clean. But usually there is a requirement to give the user feedback about what has been updated.
When i do a post followed by get i wanna show the same page with the notification about the updation being done, which makes the GET very clumsy with the extra status of whats being updated. Am i missing something here?
which is ideally very clean
debatable.
which makes the GET very clumsy with the extra status of whats being updated
...and that's one of the main reasons why.
Trying to pass transactional data via the session is a very bad practice.
The solution I've used is to use a front controller for sequences of forms (not a front controller for the whole site!) but in general trying to avoid the scenario where there is a sequence of forms to be posted

Resources