update recipe endpoint

This commit is contained in:
Kenta420-Poom 2023-09-25 16:59:10 +07:00
parent 9e8efa4cfc
commit e4fdf19c67
5 changed files with 76 additions and 14 deletions

View file

@ -143,11 +143,9 @@
</div> </div>
</aside> </aside>
<div class="px-4 pt-16 sm:ml-64"> <div class="pt-10 sm:ml-64">
<div <div class="p-4 sm:mt-5 md:mt-12">
class="p-4 border-2 border-gray-200 border-dashed rounded-lg sm:mt-5 md:mt-14" <div class="grid grid-cols-1 gap-2 mb-2">
>
<div class="grid grid-cols-1 gap-4 mb-4">
<router-outlet></router-outlet> <router-outlet></router-outlet>
</div> </div>
</div> </div>

View file

@ -4,16 +4,19 @@ import { BehaviorSubject, Observable, distinctUntilChanged } from 'rxjs';
import { Recipe } from '../models/recipe.model'; import { Recipe } from '../models/recipe.model';
import { environment } from 'src/environments/environment'; import { environment } from 'src/environments/environment';
interface Pagination { interface RecipeParams {
offset: number; offset: number;
take: number; take: number;
search: string;
} }
@Injectable({ providedIn: 'root' }) @Injectable({ providedIn: 'root' })
export class RecipeService { export class RecipeService {
constructor(private _httpClient: HttpClient) {} constructor(private _httpClient: HttpClient) {}
getRecipes(paginate: Pagination = { take: 10, offset: 0 }): Observable<{ getRecipes(
params: RecipeParams = { take: 10, offset: 0, search: '' }
): Observable<{
recipes: Recipe; recipes: Recipe;
hasMore: boolean; hasMore: boolean;
}> { }> {
@ -22,10 +25,12 @@ export class RecipeService {
hasMore: boolean; hasMore: boolean;
}>(environment.api + '/recipes', { }>(environment.api + '/recipes', {
params: { params: {
offset: String(paginate.offset), offset: params.offset,
take: String(paginate.take), take: params.take,
search: params.search,
}, },
withCredentials: true, withCredentials: true,
responseType: 'json',
}); });
} }
} }

View file

@ -28,11 +28,17 @@
<input <input
class="input input-bordered join-item w-[300px]" class="input input-bordered join-item w-[300px]"
placeholder="Product Code, Name or Other Name" placeholder="Product Code, Name or Other Name"
(input)="setSearch($event)"
(keydown.enter)="search($event)"
/> />
<button class="btn join-item">Search</button> <button class="btn join-item" (click)="search($event)">
Search
</button>
</div> </div>
<div class="flex gap-2"> <div class="flex gap-2">
<button class="btn rounded-lg">View JSON</button> <button class="btn rounded-lg" (click)="openJsonTab()">
View JSON
</button>
<button class="btn rounded-lg"> <button class="btn rounded-lg">
<svg <svg
class="w-6 h-6 text-gray-800 dark:text-white" class="w-6 h-6 text-gray-800 dark:text-white"

View file

@ -14,6 +14,7 @@ import { DatePipe, NgFor, NgIf } from '@angular/common';
import { Recipe, Recipe01 } from 'src/app/core/models/recipe.model'; import { Recipe, Recipe01 } from 'src/app/core/models/recipe.model';
import { RecipeService } from 'src/app/core/services/recipe.service'; import { RecipeService } from 'src/app/core/services/recipe.service';
import { BehaviorSubject } from 'rxjs'; import { BehaviorSubject } from 'rxjs';
import { environment } from 'src/environments/environment';
@Component({ @Component({
selector: 'app-dashboard', selector: 'app-dashboard',
@ -40,6 +41,9 @@ export class DashboardComponent implements OnInit {
isLoadMore: boolean = false; isLoadMore: boolean = false;
isHasMore: boolean = true; isHasMore: boolean = true;
private searchStr = '';
private oldSearchStr = '';
@ViewChild('table', { static: false }) set content(table: ElementRef) { @ViewChild('table', { static: false }) set content(table: ElementRef) {
table.nativeElement.addEventListener( table.nativeElement.addEventListener(
'scroll', 'scroll',
@ -56,6 +60,7 @@ export class DashboardComponent implements OnInit {
.getRecipes({ .getRecipes({
offset: this.offset, offset: this.offset,
take: this.take, take: this.take,
search: this.oldSearchStr,
}) })
.subscribe(({ recipes, hasMore }) => { .subscribe(({ recipes, hasMore }) => {
const { Recipe01, ...recipesWithoutRecipe01 } = recipes; const { Recipe01, ...recipesWithoutRecipe01 } = recipes;
@ -92,6 +97,7 @@ export class DashboardComponent implements OnInit {
.getRecipes({ .getRecipes({
offset: this.offset, offset: this.offset,
take: this.take, take: this.take,
search: this.oldSearchStr,
}) })
.subscribe(({ recipes, hasMore }) => { .subscribe(({ recipes, hasMore }) => {
const { Recipe01, ...recipesWithoutRecipe01 } = recipes; const { Recipe01, ...recipesWithoutRecipe01 } = recipes;
@ -109,4 +115,35 @@ export class DashboardComponent implements OnInit {
this.isHasMore = hasMore; this.isHasMore = hasMore;
}); });
} }
setSearch(event: Event) {
this.searchStr = (event.target as HTMLInputElement).value;
}
search(event: Event) {
this.offset = 0;
this.oldSearchStr = this.searchStr;
this._recipeService
.getRecipes({
offset: this.offset,
take: this.take,
search: this.searchStr,
})
.subscribe(({ recipes, hasMore }) => {
const { Recipe01, ...recipesWithoutRecipe01 } = recipes;
this.recipes01 = Recipe01;
this.recipes = {
...recipesWithoutRecipe01,
Recipe01: [],
};
this.offset += 10;
this.isLoaded = true;
this.isHasMore = hasMore;
});
}
openJsonTab() {
window.open(environment.api + '/recipes/json', '_blank');
}
} }

View file

@ -2,12 +2,12 @@ package routers
import ( import (
"encoding/json" "encoding/json"
"log"
"net/http" "net/http"
"recipe-manager/data" "recipe-manager/data"
"recipe-manager/models" "recipe-manager/models"
"sort" "sort"
"strconv" "strconv"
"strings"
"github.com/go-chi/chi/v5" "github.com/go-chi/chi/v5"
) )
@ -35,8 +35,20 @@ func (rr *RecipeRouter) Route(r chi.Router) {
take = newTake take = newTake
} }
// copy the strcut to avoid modifying the original
recipe := rr.data.GetRecipe() recipe := rr.data.GetRecipe()
searchQuery := r.URL.Query().Get("search")
if searchQuery != "" {
recipe.Recipe01 = []models.Recipe01{}
for _, v := range rr.data.GetRecipe01() {
if strings.Contains(strings.ToLower(v.ProductCode), strings.ToLower(searchQuery)) ||
strings.Contains(strings.ToLower(v.Name), strings.ToLower(searchQuery)) ||
strings.Contains(strings.ToLower(v.OtherName), strings.ToLower(searchQuery)) {
recipe.Recipe01 = append(recipe.Recipe01, v)
}
}
}
isHasMore := len(recipe.Recipe01) >= int(take+offset) isHasMore := len(recipe.Recipe01) >= int(take+offset)
if isHasMore { if isHasMore {
recipe.Recipe01 = recipe.Recipe01[offset : take+offset] recipe.Recipe01 = recipe.Recipe01[offset : take+offset]
@ -44,7 +56,6 @@ func (rr *RecipeRouter) Route(r chi.Router) {
return recipe.Recipe01[i].ID < recipe.Recipe01[j].ID return recipe.Recipe01[i].ID < recipe.Recipe01[j].ID
}) })
} else if len(recipe.Recipe01) > int(offset) { } else if len(recipe.Recipe01) > int(offset) {
log.Println("offset", offset, "len", len(recipe.Recipe01))
recipe.Recipe01 = recipe.Recipe01[offset:] recipe.Recipe01 = recipe.Recipe01[offset:]
} else { } else {
recipe.Recipe01 = []models.Recipe01{} recipe.Recipe01 = []models.Recipe01{}
@ -55,5 +66,10 @@ func (rr *RecipeRouter) Route(r chi.Router) {
"hasMore": isHasMore, "hasMore": isHasMore,
}) })
}) })
r.Get("/json", func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "application/json")
json.NewEncoder(w).Encode(rr.data.GetRecipe())
})
}) })
} }