feat: crud service
This commit is contained in:
@@ -1 +1 @@
|
|||||||
7f72a57db80d176e546b9dc65909aede
|
47eee1634da5a53f29dddd3801e51ad
|
||||||
|
|||||||
@@ -1 +1 @@
|
|||||||
cda88f3dc4dd41756cb47884735846ff
|
7fb10105afd7388bbe8ff39ed98ded4e
|
||||||
|
|||||||
4
frontend/bindings/app/internal/models/index.ts
Normal file
4
frontend/bindings/app/internal/models/index.ts
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
// Cynhyrchwyd y ffeil hon yn awtomatig. PEIDIWCH Â MODIWL
|
||||||
|
// This file is automatically generated. DO NOT EDIT
|
||||||
|
|
||||||
|
export * from "./models.js";
|
||||||
31
frontend/bindings/app/internal/models/models.ts
Normal file
31
frontend/bindings/app/internal/models/models.ts
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
// Cynhyrchwyd y ffeil hon yn awtomatig. PEIDIWCH Â MODIWL
|
||||||
|
// This file is automatically generated. DO NOT EDIT
|
||||||
|
|
||||||
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
||||||
|
// @ts-ignore: Unused imports
|
||||||
|
import {Create as $Create} from "@wailsio/runtime";
|
||||||
|
|
||||||
|
export class Post {
|
||||||
|
"Id": number;
|
||||||
|
"Text": string;
|
||||||
|
|
||||||
|
/** Creates a new Post instance. */
|
||||||
|
constructor($$source: Partial<Post> = {}) {
|
||||||
|
if (!("Id" in $$source)) {
|
||||||
|
this["Id"] = 0;
|
||||||
|
}
|
||||||
|
if (!("Text" in $$source)) {
|
||||||
|
this["Text"] = "";
|
||||||
|
}
|
||||||
|
|
||||||
|
Object.assign(this, $$source);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new Post instance from a string or object.
|
||||||
|
*/
|
||||||
|
static createFrom($$source: any = {}): Post {
|
||||||
|
let $$parsedSource = typeof $$source === 'string' ? JSON.parse($$source) : $$source;
|
||||||
|
return new Post($$parsedSource as Partial<Post>);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,7 +1,9 @@
|
|||||||
// Cynhyrchwyd y ffeil hon yn awtomatig. PEIDIWCH Â MODIWL
|
// Cynhyrchwyd y ffeil hon yn awtomatig. PEIDIWCH Â MODIWL
|
||||||
// This file is automatically generated. DO NOT EDIT
|
// This file is automatically generated. DO NOT EDIT
|
||||||
|
|
||||||
import * as GreetService from "./greetservice.js";
|
import * as PostService from "./postservice.js";
|
||||||
export {
|
export {
|
||||||
GreetService
|
PostService
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export * from "./models.js";
|
||||||
|
|||||||
13
frontend/bindings/app/internal/services/models.ts
Normal file
13
frontend/bindings/app/internal/services/models.ts
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
// Cynhyrchwyd y ffeil hon yn awtomatig. PEIDIWCH Â MODIWL
|
||||||
|
// This file is automatically generated. DO NOT EDIT
|
||||||
|
|
||||||
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
||||||
|
// @ts-ignore: Unused imports
|
||||||
|
import {Create as $Create} from "@wailsio/runtime";
|
||||||
|
|
||||||
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
||||||
|
// @ts-ignore: Unused imports
|
||||||
|
import * as models$0 from "../models/models.js";
|
||||||
|
|
||||||
|
export const Post = models$0.Post;
|
||||||
|
export type Post = models$0.Post;
|
||||||
69
frontend/bindings/app/internal/services/postservice.ts
Normal file
69
frontend/bindings/app/internal/services/postservice.ts
Normal file
@@ -0,0 +1,69 @@
|
|||||||
|
// Cynhyrchwyd y ffeil hon yn awtomatig. PEIDIWCH Â MODIWL
|
||||||
|
// This file is automatically generated. DO NOT EDIT
|
||||||
|
|
||||||
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
||||||
|
// @ts-ignore: Unused imports
|
||||||
|
import {Call as $Call, Create as $Create} from "@wailsio/runtime";
|
||||||
|
|
||||||
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
||||||
|
// @ts-ignore: Unused imports
|
||||||
|
import * as models$0 from "../models/models.js";
|
||||||
|
|
||||||
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
||||||
|
// @ts-ignore: Unused imports
|
||||||
|
import * as $models from "./models.js";
|
||||||
|
|
||||||
|
export function Count(): Promise<number> & { cancel(): void } {
|
||||||
|
let $resultPromise = $Call.ByID(3109924027) as any;
|
||||||
|
return $resultPromise;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function Create(item: $models.Post): Promise<$models.Post> & { cancel(): void } {
|
||||||
|
let $resultPromise = $Call.ByID(1443399856, item) as any;
|
||||||
|
let $typingPromise = $resultPromise.then(($result) => {
|
||||||
|
return $$createType0($result);
|
||||||
|
}) as any;
|
||||||
|
$typingPromise.cancel = $resultPromise.cancel.bind($resultPromise);
|
||||||
|
return $typingPromise;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function Delete(item: $models.Post): Promise<$models.Post> & { cancel(): void } {
|
||||||
|
let $resultPromise = $Call.ByID(2924549135, item) as any;
|
||||||
|
let $typingPromise = $resultPromise.then(($result) => {
|
||||||
|
return $$createType0($result);
|
||||||
|
}) as any;
|
||||||
|
$typingPromise.cancel = $resultPromise.cancel.bind($resultPromise);
|
||||||
|
return $typingPromise;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function GetAll(): Promise<($models.Post | null)[]> & { cancel(): void } {
|
||||||
|
let $resultPromise = $Call.ByID(65691059) as any;
|
||||||
|
let $typingPromise = $resultPromise.then(($result) => {
|
||||||
|
return $$createType2($result);
|
||||||
|
}) as any;
|
||||||
|
$typingPromise.cancel = $resultPromise.cancel.bind($resultPromise);
|
||||||
|
return $typingPromise;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function GetById(id: number): Promise<$models.Post | null> & { cancel(): void } {
|
||||||
|
let $resultPromise = $Call.ByID(4074736792, id) as any;
|
||||||
|
let $typingPromise = $resultPromise.then(($result) => {
|
||||||
|
return $$createType1($result);
|
||||||
|
}) as any;
|
||||||
|
$typingPromise.cancel = $resultPromise.cancel.bind($resultPromise);
|
||||||
|
return $typingPromise;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function Update(item: $models.Post): Promise<$models.Post> & { cancel(): void } {
|
||||||
|
let $resultPromise = $Call.ByID(137798821, item) as any;
|
||||||
|
let $typingPromise = $resultPromise.then(($result) => {
|
||||||
|
return $$createType0($result);
|
||||||
|
}) as any;
|
||||||
|
$typingPromise.cancel = $resultPromise.cancel.bind($resultPromise);
|
||||||
|
return $typingPromise;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Private type creation functions
|
||||||
|
const $$createType0 = models$0.Post.createFrom;
|
||||||
|
const $$createType1 = $Create.Nullable($$createType0);
|
||||||
|
const $$createType2 = $Create.Array($$createType1);
|
||||||
@@ -1,8 +0,0 @@
|
|||||||
package internal
|
|
||||||
|
|
||||||
// TODO: implement service and migrator
|
|
||||||
type GreetService struct{}
|
|
||||||
|
|
||||||
func (g *GreetService) Greet(name string) string {
|
|
||||||
return "Hello " + name + "!"
|
|
||||||
}
|
|
||||||
52
internal/services/post_service_crud.go
Normal file
52
internal/services/post_service_crud.go
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
package services
|
||||||
|
|
||||||
|
import (
|
||||||
|
"app/internal/dal"
|
||||||
|
"app/internal/models"
|
||||||
|
"errors"
|
||||||
|
|
||||||
|
"gorm.io/gen/field"
|
||||||
|
"gorm.io/gorm"
|
||||||
|
)
|
||||||
|
|
||||||
|
type PostService struct{}
|
||||||
|
type Post = models.Post
|
||||||
|
|
||||||
|
func (service *PostService) Create(item Post) (Post, error) {
|
||||||
|
err := dal.Post.Preload(field.Associations).Create(&item)
|
||||||
|
return item, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (service *PostService) GetAll() ([]*Post, error) {
|
||||||
|
var posts []*Post
|
||||||
|
posts, err := dal.Post.Preload(field.Associations).Find()
|
||||||
|
return posts, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (service *PostService) GetById(id uint) (*Post, error) {
|
||||||
|
item, err := dal.Post.Preload(field.Associations).Where(dal.Post.Id.Eq(id)).First()
|
||||||
|
if err != nil {
|
||||||
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||||
|
return nil, nil
|
||||||
|
} else {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return item, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (service *PostService) Update(item Post) (Post, error) {
|
||||||
|
err := dal.Post.Preload(field.Associations).Save(&item)
|
||||||
|
return item, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (service *PostService) Delete(item Post) (Post, error) {
|
||||||
|
_, err := dal.Post.Unscoped().Preload(field.Associations).Delete(&item)
|
||||||
|
return item, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (service *PostService) Count() (int64, error) {
|
||||||
|
amount, err := dal.Post.Count()
|
||||||
|
return amount, err
|
||||||
|
}
|
||||||
2
main.go
2
main.go
@@ -15,7 +15,7 @@ func main() {
|
|||||||
Name: "nto_starterkit",
|
Name: "nto_starterkit",
|
||||||
Description: "A demo of using raw HTML & CSS",
|
Description: "A demo of using raw HTML & CSS",
|
||||||
Services: []application.Service{
|
Services: []application.Service{
|
||||||
application.NewService(&internal.GreetService{}),
|
application.NewService(&services.PostService{}),
|
||||||
},
|
},
|
||||||
Assets: application.AssetOptions{
|
Assets: application.AssetOptions{
|
||||||
Handler: application.AssetFileServerFS(assets),
|
Handler: application.AssetFileServerFS(assets),
|
||||||
|
|||||||
Reference in New Issue
Block a user