Feature/Auth: implement user authentication #3
@@ -17,7 +17,7 @@ public class ItemsController : ControllerBase {
|
|||||||
|
|
||||||
[AllowAnonymous] // accessible if not authorized
|
[AllowAnonymous] // accessible if not authorized
|
||||||
[HttpGet]
|
[HttpGet]
|
||||||
public async Task<ActionResult<List<Item>>> getItemss() {
|
public async Task<ActionResult<List<Item>>> getItems() {
|
||||||
return Ok(await service_.GetAll());
|
return Ok(await service_.GetAll());
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -34,9 +34,16 @@ public class ItemsController : ControllerBase {
|
|||||||
|
|
||||||
[Authorize] // testing the authorization
|
[Authorize] // testing the authorization
|
||||||
[HttpPost]
|
[HttpPost]
|
||||||
public async Task<ActionResult<Item>> createItem(Item item) {
|
public async Task<ActionResult<Item>> createItem(ItemDto item) {
|
||||||
|
|
||||||
var created = await service_.Create(item);
|
Item newItem = new Item {
|
||||||
|
Name = item.Name,
|
||||||
|
Description = item.Description,
|
||||||
|
CreatedAt = DateTime.UtcNow,
|
||||||
|
LastEditedAt = DateTime.UtcNow
|
||||||
|
};
|
||||||
|
|
||||||
|
var created = await service_.Create(newItem);
|
||||||
|
|
||||||
return CreatedAtAction(
|
return CreatedAtAction(
|
||||||
nameof(getItem),
|
nameof(getItem),
|
||||||
@@ -47,9 +54,20 @@ public class ItemsController : ControllerBase {
|
|||||||
|
|
||||||
[Authorize]
|
[Authorize]
|
||||||
[HttpPut("{id}")]
|
[HttpPut("{id}")]
|
||||||
public async Task<ActionResult<Item>> updateItem(int id, Item item) {
|
public async Task<ActionResult<Item>> updateItem(int id, ItemDto item) {
|
||||||
|
|
||||||
var updated = await service_.Update(item);
|
Item? oldItem = await service_.Get(id);
|
||||||
|
if(oldItem == null) return NotFound();
|
||||||
|
|
||||||
|
Item updatedItem = new Item {
|
||||||
|
Id = oldItem.Id,
|
||||||
|
Name = item.Name,
|
||||||
|
Description = item.Description,
|
||||||
|
CreatedAt = oldItem.CreatedAt,
|
||||||
|
LastEditedAt = DateTime.UtcNow
|
||||||
|
};
|
||||||
|
|
||||||
|
var updated = await service_.Update(updatedItem);
|
||||||
|
|
||||||
if (updated == null) return NotFound();
|
if (updated == null) return NotFound();
|
||||||
|
|
||||||
|
|||||||
@@ -3,7 +3,7 @@
|
|||||||
// this item service will handle all to <-> from the server when handling item objects
|
// this item service will handle all to <-> from the server when handling item objects
|
||||||
|
|
||||||
import api from "./axios.ts"
|
import api from "./axios.ts"
|
||||||
import type { Item } from "../models/Item.ts";
|
import type { Item, ItemDto } from "../models/Item.ts";
|
||||||
|
|
||||||
const API_URL: string = "/items";
|
const API_URL: string = "/items";
|
||||||
|
|
||||||
@@ -11,8 +11,8 @@ export const getItems = () => api.get<Item[]>(`${API_URL}`);
|
|||||||
|
|
||||||
export const getItem = (id: number) => api.get<Item>(`${API_URL}/${id}`);
|
export const getItem = (id: number) => api.get<Item>(`${API_URL}/${id}`);
|
||||||
|
|
||||||
export const createItem = (data: Item) => api.post<Item>(`${API_URL}`, data);
|
export const createItem = (data: ItemDto) => api.post<Item>(`${API_URL}`, data);
|
||||||
|
|
||||||
export const updateItem = (id: number, data: Item) => api.put<Item>(`${API_URL}/${id}`, data);
|
export const updateItem = (id: number, data: ItemDto) => api.put<Item>(`${API_URL}/${id}`, data);
|
||||||
|
|
||||||
export const deleteItem = (id: number) => api.delete<Item>(`${API_URL}/${id}`);
|
export const deleteItem = (id: number) => api.delete<Item>(`${API_URL}/${id}`);
|
||||||
|
|||||||
@@ -7,13 +7,7 @@ export interface Item {
|
|||||||
lastEditedAt: string;
|
lastEditedAt: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface RegisterDto {
|
export interface ItemDto {
|
||||||
username: string;
|
name: string;
|
||||||
email: string;
|
description: string;
|
||||||
password: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface LoginDto {
|
|
||||||
username: string;
|
|
||||||
password: string;
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -48,7 +48,7 @@ async function save(): Promise<void> {
|
|||||||
|
|
||||||
<form @submit.prevent="save">
|
<form @submit.prevent="save">
|
||||||
<input v-model="item.name" placeholder="Name" />
|
<input v-model="item.name" placeholder="Name" />
|
||||||
|
<input v-model="item.description" placeholder="Name" />
|
||||||
<button type="submit">Save</button>
|
<button type="submit">Save</button>
|
||||||
</form>
|
</form>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -5,7 +5,7 @@
|
|||||||
// sighh
|
// sighh
|
||||||
|
|
||||||
import { defineStore } from "pinia";
|
import { defineStore } from "pinia";
|
||||||
import type { Item } from "../models/Item.ts";
|
import type { Item, ItemDto } from "../models/Item.ts";
|
||||||
import * as itemsApi from "../api/ItemsApi";
|
import * as itemsApi from "../api/ItemsApi";
|
||||||
|
|
||||||
interface ItemState {
|
interface ItemState {
|
||||||
@@ -28,15 +28,15 @@ export const useItemsStore = defineStore("items", {
|
|||||||
this.loading = false;
|
this.loading = false;
|
||||||
},
|
},
|
||||||
|
|
||||||
async addItem(item: Item) {
|
async addItem(item: ItemDto) {
|
||||||
const response = await itemsApi.createItem(item);
|
const response = await itemsApi.createItem(item);
|
||||||
this.items.push(response.data);
|
this.items.push(response.data);
|
||||||
},
|
},
|
||||||
|
|
||||||
async updateItem(id: number, item: Item) {
|
async updateItem(id: number, item: ItemDto) {
|
||||||
await itemsApi.updateItem(id, item);
|
const response = await itemsApi.updateItem(id, item);
|
||||||
const index = this.items.findIndex(i => i.id === id);
|
const index = this.items.findIndex(i => i.id === id);
|
||||||
this.items[index] = item;
|
this.items[index] = response.data;
|
||||||
},
|
},
|
||||||
|
|
||||||
async removeItem(id: number) {
|
async removeItem(id: number) {
|
||||||
|
|||||||
Reference in New Issue
Block a user