N
N
nano_e_t_42019-02-02 15:13:26
go
nano_e_t_4, 2019-02-02 15:13:26

How to build application architecture?

Hello everyone
. I am periodically engaged in development, and recently I was asked to make a simple backend for a cafe service. The bottom line is that the page should display dishes with photos.
From the side of the backend, I made the functionality of adding / changing / deleting data from databases and wrote an http wrapper for this. But I don't like the current architecture, namely:
There is a food structure:

type Food struct {
  Id 				int			`validate:"required"	gorm:"primary_key:true"`
        Name 			string		`validate:"required" 	gorm:"column:name;unique"`
       ....
}

There is a Category structure (soups\salads\drinks\hot dishes and so on):
type Categories struct {
  Id 				int		`gorm:"primary_key:true"`
        Name 			string		`validate:"required" 	gorm:"column:name;unique"`
        ....
}

Now the implementation is such that there are separate functions that add/modify/delete records in the database and there are apips for each of the functions, that is:
server_api:
r.Route("/food/", func (r chi.Router) {
      r.Post("/add", AddFood)
      r.Post("/update", UpdateFood)
      r.Post("/delete", DeleteFood)
    })

    r.Route("/category/", func (r chi.Router) {
      r.Post("/add", AddCategorie)
      r.Post("/update", UpdateCategorie)
      r.Post("/delete", DeleteCategorie)
    })

controllers:
func AddFood|AddCategorie(w http.ResponseWriter, r *http.Request) {
       result := food|categorie.Validate()
       food|categorie.Save()
}

func UpdateFood|UpdateCategorie(w http.ResponseWriter, r *http.Request) {
       result := food|categorie.Validate()
       food|categorie.Update()
}

func DeleteFood|DeleteCategorie(w http.ResponseWriter, r *http.Request) {
}

and the logic of direct interaction with the database:
func (f Food|Categorie) Save() (result Result){
return
}
func (f Food|Categorie) Update() (result Result){
  return
}
func (f Food|Categorie) Delete() (result Result){
  return
}

but the fact of a large amount of code duplication (including the duplication of the validation function) is very confusing. Because the logic of both structures is very identical. Tell me how you can implement all this less voluminously
.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
grinat, 2019-02-02
@grinat

And what for do you rest on go for this?) Based on the technical specification, even just html files will suffice. Go is for something highly loaded, and not because it is fashionable, it will work with orm in terms of speed at the level of some thread of pehepe, because for example they all do mapping, pull relations, etc. through reflexes, different crutches. Now, if you need to tear out a million records from the database and do terrible and terrible things with them, then this will be revealed.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question