public async Task <Response> DoModelAsync(Request request) { Response response = new Response(); // A dict where I store the operator so i can append the result and we can later change ISearchOperator in the request to an List of operators. Dictionary <ISearchOperator, CreatorProfile> resultDict = new Dictionary <ISearchOperator, CreatorProfile>(); switch (request.SearchOperator) { case ISearchOperator.NAME: // TODO: Change so that it responses with more that are close to the result username CreatorProfile userResult = _context.CreatorProfiles.FirstOrDefault(x => x.Name == request.SearchQuery); resultDict.Add(request.SearchOperator, userResult); break; case ISearchOperator.ID: CreatorProfile resultId = await _context.CreatorProfiles.FindAsync(request.SearchQuery); resultDict.Add(request.SearchOperator, resultId); break; default: break; } return(new Response { // if empty is notSucceeded isSucceeded = resultDict.ElementAt(0).Value != null, Users = resultDict.ElementAt(0).Value }); }
public async Task <Response> DoModelAsync(Request request) { Response response = new Response(); // A dict where I store the operator so i can append the result and we can later change ISearchOperator in the request to an List of operators. Dictionary <ISearchOperator, CreatorProfile> resultDict = new Dictionary <ISearchOperator, CreatorProfile>(); switch (request.SearchOperator) { case ISearchOperator.NAME: // TODO: Change so that it responses with more that are close to the result username CreatorProfile userResult = _context.CreatorProfiles.FirstOrDefault(x => x.Name == request.SearchQuery); response.Creators.Add(userResult); break; case ISearchOperator.ID: CreatorProfile resultId = await _context.CreatorProfiles.FindAsync(request.SearchQuery); response.Creators.Add(resultId); break; case ISearchOperator.ALL: var resultAll = _context.CreatorProfiles.ToList(); response.Creators.AddRange(resultAll); break; default: break; } if (response.Creators.Count == 0) { response.isSucceeded = false; } return(response); }
public async Task <IActionResult> Put(string id, CreatorProfile profile) { var dbProfile = this.ctx.CreatorProfiles.FirstOrDefault(p => p.Id == id); if (!dbProfile?.CanEdit(await this.userManager.GetUserAsync(User)) ?? true) { throw new InvalidOperationException(this.errorMessagelocalizer["You can't edit the profile {0}!", id]); } profile.Id = id; this.ctx.CreatorProfiles.Update(profile); await this.ctx.SaveChangesAsync(); return(NoContent()); }
public async Task <ActionResult <CreatorProfile> > Post(CreatorProfile profile) { profile.Owner = await this.userManager.GetUserAsync(User); profile.Id = null; if (profile.Owner.Profile != null) { throw new InvalidOperationException(this.errorMessagelocalizer["You can't create two Profiles!"]); } profile.Owner.Profile = profile; this.ctx.Users.Update(profile.Owner); await this.ctx.CreatorProfiles.AddAsync(profile); await this.ctx.SaveChangesAsync(); return(CreatedAtAction(nameof(Get), new { id = profile.Id }, profile)); }