Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
D
Demo2019s
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Operations
Operations
Incidents
Environments
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
icd0009-2019s
Demo2019s
Commits
9fa4c5da
Commit
9fa4c5da
authored
Mar 29, 2020
by
Andres Käver
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
initial owner rest controller, no dtos
parent
49e19610
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
25 additions
and
32 deletions
+25
-32
AspSolution/WebApp/ApiControllers/OwnersController.cs
AspSolution/WebApp/ApiControllers/OwnersController.cs
+24
-31
AspSolution/WebApp/Controllers/OwnersController.cs
AspSolution/WebApp/Controllers/OwnersController.cs
+1
-1
No files found.
AspSolution/WebApp/ApiControllers/OwnersController.cs
View file @
9fa4c5da
...
...
@@ -2,6 +2,7 @@ using System;
using
System.Collections.Generic
;
using
System.Linq
;
using
System.Threading.Tasks
;
using
Contracts.DAL.App
;
using
Microsoft.AspNetCore.Http
;
using
Microsoft.AspNetCore.Mvc
;
using
Microsoft.EntityFrameworkCore
;
...
...
@@ -18,38 +19,36 @@ namespace WebApp.ApiControllers
[
Authorize
(
AuthenticationSchemes
=
JwtBearerDefaults
.
AuthenticationScheme
)]
public
class
OwnersController
:
ControllerBase
{
private
readonly
AppDbContext
_context
;
private
readonly
IAppUnitOfWork
_uow
;
public
OwnersController
(
AppDbContext
context
)
public
OwnersController
(
IAppUnitOfWork
uow
)
{
_
context
=
context
;
_
uow
=
uow
;
}
// GET: api/Owners
[
HttpGet
]
public
async
Task
<
ActionResult
<
IEnumerable
<
Owner
>>>
GetOwners
()
{
return
await
_context
.
Owners
.
Where
(
o
=>
o
.
AppUserId
==
User
.
UserGuidId
()).
ToListAsync
();
var
owners
=
await
_uow
.
Owners
.
AllAsync
(
User
.
UserGuidId
());
return
Ok
(
owners
);
}
// GET: api/Owners/5
[
HttpGet
(
"{id}"
)]
public
async
Task
<
ActionResult
<
Owner
>>
GetOwner
(
Guid
id
)
{
var
owner
=
await
_context
.
Owners
.
FirstOrDefaultAsync
(
o
=>
o
.
Id
==
id
&&
o
.
AppUserId
==
User
.
UserGuidId
());
var
owner
=
await
_uow
.
Owners
.
FirstOrDefaultAsync
(
id
,
User
.
UserGuidId
());
if
(
owner
==
null
)
{
return
NotFound
();
}
return
owner
;
return
Ok
(
owner
)
;
}
// PUT: api/Owners/5
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see https://aka.ms/RazorPagesCRUD.
[
HttpPut
(
"{id}"
)]
public
async
Task
<
IActionResult
>
PutOwner
(
Guid
id
,
Owner
owner
)
{
...
...
@@ -58,58 +57,52 @@ namespace WebApp.ApiControllers
return
BadRequest
();
}
_context
.
Entry
(
owner
).
State
=
EntityState
.
Modified
;
owner
.
AppUserId
=
User
.
UserGuidId
();
_uow
.
Owners
.
Update
(
owner
);
try
{
await
_
context
.
SaveChangesAsync
();
await
_
uow
.
SaveChangesAsync
();
}
catch
(
DbUpdateConcurrencyException
)
{
if
(!
OwnerExists
(
id
))
if
(!
await
_uow
.
Owners
.
ExistsAsync
(
id
,
User
.
UserGuidId
()
))
{
return
NotFound
();
}
else
{
throw
;
}
throw
;
}
return
NoContent
();
}
// POST: api/Owners
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see https://aka.ms/RazorPagesCRUD.
[
HttpPost
]
public
async
Task
<
ActionResult
<
Owner
>>
PostOwner
(
Owner
owner
)
{
_context
.
Owners
.
Add
(
owner
);
await
_context
.
SaveChangesAsync
();
owner
.
AppUserId
=
User
.
UserGuidId
();
_uow
.
Owners
.
Add
(
owner
);
await
_uow
.
SaveChangesAsync
();
return
CreatedAtAction
(
"GetOwner"
,
new
{
id
=
owner
.
Id
},
owner
);
return
CreatedAtAction
(
"GetOwner"
,
new
{
id
=
owner
.
Id
},
owner
);
}
// DELETE: api/Owners/5
[
HttpDelete
(
"{id}"
)]
public
async
Task
<
ActionResult
<
Owner
>>
DeleteOwner
(
Guid
id
)
{
var
owner
=
await
_
context
.
Owners
.
FindAsync
(
id
);
var
owner
=
await
_
uow
.
Owners
.
FirstOrDefaultAsync
(
id
,
User
.
UserGuidId
()
);
if
(
owner
==
null
)
{
return
NotFound
();
}
_
context
.
Owners
.
Remove
(
owner
);
await
_
context
.
SaveChangesAsync
();
_
uow
.
Owners
.
Remove
(
owner
);
await
_
uow
.
SaveChangesAsync
();
return
owner
;
}
private
bool
OwnerExists
(
Guid
id
)
{
return
_context
.
Owners
.
Any
(
e
=>
e
.
Id
==
id
);
return
Ok
(
owner
);
}
}
}
}
\ No newline at end of file
AspSolution/WebApp/Controllers/OwnersController.cs
View file @
9fa4c5da
...
...
@@ -13,7 +13,7 @@ using Microsoft.AspNetCore.Authorization;
namespace
WebApp.Controllers
{
[
Authorize
(
Roles
=
"User"
)
]
[
Authorize
]
public
class
OwnersController
:
Controller
{
private
readonly
IAppUnitOfWork
_uow
;
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment