|
1 | | -using Microsoft.AspNetCore.Mvc; |
| 1 | +using Microsoft.AspNetCore.Http.Internal; |
| 2 | +using Microsoft.AspNetCore.Mvc; |
2 | 3 | using NetCoreStack.Contracts; |
3 | 4 | using NetCoreStack.Data.Interfaces; |
4 | 5 | using NetCoreStack.Domain.Contracts; |
5 | 6 | using NetCoreStack.Domain.Contracts.ApiContracts; |
6 | 7 | using NetCoreStack.Mvc.Extensions; |
| 8 | +using System.IO; |
7 | 9 | using System.Linq; |
| 10 | +using System.Text; |
8 | 11 | using System.Threading.Tasks; |
9 | 12 |
|
10 | 13 | namespace NetCoreStack.WebClient.Hosting.Controllers |
@@ -99,6 +102,43 @@ public async Task<AlbumViewModel> SaveAlbum([FromBody]AlbumViewModel model) |
99 | 102 | return model; |
100 | 103 | } |
101 | 104 |
|
| 105 | + [HttpPost(nameof(SaveAlbumSubmit))] |
| 106 | + public async Task<AlbumViewModel> SaveAlbumSubmit(AlbumViewModelSubmit model) |
| 107 | + { |
| 108 | + await Task.CompletedTask; |
| 109 | + var req = HttpContext.Request; |
| 110 | + req.EnableRewind(); |
| 111 | + |
| 112 | + // Arguments: Stream, Encoding, detect encoding, buffer size |
| 113 | + // AND, the most important: keep stream opened |
| 114 | + using (StreamReader reader = new StreamReader(req.Body, Encoding.UTF8, true, 1024, true)) |
| 115 | + { |
| 116 | + var bodyStr = reader.ReadToEnd(); |
| 117 | + } |
| 118 | + |
| 119 | + long id = 0; |
| 120 | + var objectState = ObjectState.Added; |
| 121 | + if (!model.IsNew) |
| 122 | + { |
| 123 | + id = model.Id; |
| 124 | + objectState = ObjectState.Modified; |
| 125 | + } |
| 126 | + |
| 127 | + var album = new Album |
| 128 | + { |
| 129 | + AlbumArtUrl = model.AlbumArtUrl, |
| 130 | + ArtistId = model.ArtistId, |
| 131 | + GenreId = model.GenreId, |
| 132 | + Id = id, |
| 133 | + ObjectState = objectState, |
| 134 | + Price = model.Price, |
| 135 | + Title = model.Title |
| 136 | + }; |
| 137 | + |
| 138 | + _unitOfWork.Repository<Album>().SaveAllChanges(album); |
| 139 | + return model; |
| 140 | + } |
| 141 | + |
102 | 142 | [HttpPut(nameof(UpdateAlbum))] |
103 | 143 | public async Task<AlbumViewModel> UpdateAlbum(long id, [FromBody]AlbumViewModel model) |
104 | 144 | { |
|
0 commit comments