Skip to content

Commit 148caeb

Browse files
committed
fixed feature create
1 parent 5d62997 commit 148caeb

File tree

7 files changed

+34
-11
lines changed

7 files changed

+34
-11
lines changed

rubberduckvba.Server/Api/Features/FeaturesController.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ public async Task<ActionResult<FeatureEditViewModel>> Create([FromQuery] Reposit
159159
return Ok(model);
160160
}
161161

162-
[HttpPost("create")]
162+
[HttpPost("features/create")]
163163
[Authorize("github")]
164164
public async Task<ActionResult<FeatureEditViewModel>> Create([FromBody] FeatureEditViewModel model)
165165
{
@@ -168,7 +168,7 @@ public async Task<ActionResult<FeatureEditViewModel>> Create([FromBody] FeatureE
168168
return BadRequest("Model is invalid for this endpoint.");
169169
}
170170

171-
var existing = await db.ResolveFeature(model.RepositoryId, model.Name);
171+
var existing = await db.GetFeatureId(model.RepositoryId, model.Name);
172172
if (existing != null)
173173
{
174174
return BadRequest($"Model [Name] must be unique; feature '{model.Name}' already exists.");
@@ -205,6 +205,8 @@ public async Task<ActionResult<FeatureEditViewModel>> Update([FromBody] FeatureE
205205
return new FeatureEditViewModel(result, features, RepositoryOptions);
206206
}
207207

208+
209+
208210
[HttpPost("markdown/format")]
209211
public MarkdownContentViewModel FormatMarkdown([FromBody] MarkdownContentViewModel model)
210212
{

rubberduckvba.Server/Data/Repository.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ namespace rubberduckvba.Server.Data;
99

1010
public interface IRepository<TEntity> where TEntity : Entity
1111
{
12+
bool TryGetId(string name, out int id);
1213
int GetId(string name);
1314
TEntity GetById(int id);
1415
IEnumerable<TEntity> GetAll();
@@ -71,6 +72,20 @@ ParentFKColumnName is null || !parentId.HasValue
7172
? GetAll()
7273
: Query(db => db.Query<TEntity>($"{SelectSql} WHERE a.[{ParentFKColumnName}]=@parentId", new { parentId }));
7374

75+
public virtual bool TryGetId(string name, out int id)
76+
{
77+
id = default;
78+
79+
var result = Get(db => db.QuerySingleOrDefault<int?>($"SELECT [Id] FROM [dbo].[{TableName}] WHERE [Name]=@name", new { name }));
80+
if (result.HasValue)
81+
{
82+
id = result.Value;
83+
return true;
84+
}
85+
86+
return false;
87+
}
88+
7489
public virtual int GetId(string name) => Get(db => db.QuerySingle<int>($"SELECT [Id] FROM [dbo].[{TableName}] WHERE [Name]=@name", new { name }));
7590
public virtual TEntity GetById(int id) => Get(db => db.QuerySingle<TEntity>(SelectSql + " WHERE a.[Id]=@id", new { id }));
7691
public virtual TEntity Insert(TEntity entity) => Insert([entity]).Single();

rubberduckvba.Server/Services/rubberduckdb/FeatureServices.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public class FeatureServices(
1111
IRepository<QuickFixEntity> quickfixRepository,
1212
IRepository<AnnotationEntity> annotationRepository)
1313
{
14-
public int GetId(string name) => featureRepository.GetId(name);
14+
public int? GetId(string name) => featureRepository.TryGetId(name, out var id) ? id : null;
1515
public IEnumerable<Feature> Get(bool topLevelOnly = true)
1616
{
1717
return featureRepository.GetAll()

rubberduckvba.client/src/app/components/edit-feature/edit-feature.component.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ <h6>Edit {{action == 'edit' ? 'description' : 'summary'}}</h6>
4040
</div>
4141
<div class="row">
4242
<span *ngIf="action == 'edit'" class="text-small text-muted text-end">{{feature.description.length}}</span>
43-
<span *ngIf="action == 'summary'" class="text-small text-muted text-end">{{feature.shortDescription?.length ?? 0}}</span>
44-
</div>
43+
<span *ngIf="action == 'summary'" class="text-small text-muted text-end">{{feature.shortDescription.length}}</span>
44+
</div>x
4545
<div *ngIf="action == 'edit'">
4646
<hr />
4747
<button type="button" class="btn btn-outline-secondary mb-2" (click)="onPreviewDescription()">

rubberduckvba.client/src/app/components/edit-feature/edit-feature.component.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,12 @@ export class EditFeatureComponent implements OnInit, OnChanges {
8080
dateInserted: '',
8181
dateUpdated: '',
8282
description: '',
83-
id: 0,
83+
id: undefined,
8484
isHidden: false,
8585
isNew: false,
8686
name: 'NewFeature1',
8787
title: 'New Feature',
88+
shortDescription: '',
8889
featureId: parentId,
8990
featureName: parentName,
9091
featureTitle: parentTitle,
@@ -105,7 +106,7 @@ export class EditFeatureComponent implements OnInit, OnChanges {
105106

106107
public onConfirmCreate(): void {
107108
this.modal.dismissAll();
108-
this.api.saveFeature(this.subfeature).subscribe(saved => {
109+
this.api.createFeature(this.subfeature).subscribe(saved => {
109110
window.location.reload();
110111
});
111112
}

rubberduckvba.client/src/app/model/feature.model.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
export interface ViewModel {
2-
id: number;
2+
id: number | undefined;
33
dateInserted: string;
44
dateUpdated: string;
55
name: string;
@@ -21,7 +21,7 @@ export interface SubFeatureViewModel extends ViewModel {
2121

2222
title: string;
2323
description: string;
24-
shortDescription?: string | undefined;
24+
shortDescription: string;
2525
}
2626

2727
export interface XmlDocViewModel extends SubFeatureViewModel {
@@ -190,7 +190,7 @@ export interface AnnotationViewModel extends XmlDocViewModel {
190190
export type XmlDocItemViewModel = InspectionViewModel | QuickFixViewModel | AnnotationViewModel;
191191

192192
export class ViewModelBase implements ViewModel {
193-
id: number;
193+
id: number | undefined;
194194
dateInserted: string;
195195
dateUpdated: string;
196196
name: string;
@@ -266,7 +266,7 @@ export class SubFeatureViewModelClass extends ViewModelBase implements SubFeatur
266266
featureTitle?: string | undefined;
267267
title: string;
268268
description: string;
269-
shortDescription?: string | undefined;
269+
shortDescription: string;
270270

271271
constructor(model: SubFeatureViewModel) {
272272
super(model);

rubberduckvba.client/src/app/services/api-client.service.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,11 @@ export class ApiClientService {
8383
}));
8484
}
8585

86+
public createFeature(model: SubFeatureViewModel): Observable<SubFeatureViewModel> {
87+
const url = `${environment.apiBaseUrl}features/create`;
88+
return this.data.postAsync<SubFeatureViewModel, SubFeatureViewModel>(url, model).pipe(map(result => new SubFeatureViewModelClass(result as SubFeatureViewModel)));
89+
}
90+
8691
public saveFeature(model: SubFeatureViewModel): Observable<SubFeatureViewModel> {
8792
const url = `${environment.apiBaseUrl}features/update`;
8893
return this.data.postAsync<SubFeatureViewModel, SubFeatureViewModel>(url, model).pipe(map(result => new SubFeatureViewModelClass(result as SubFeatureViewModel)));

0 commit comments

Comments
 (0)