public async Task <ActionResult> AddAvatar([FromBody] AddAvatarToUserDto request) { var user = _userService.GetById(request.UserId); var media = await _ctx.Media.FindAsync(request.MediaId); if (user == null || media == null) { return(NotFound()); } if (media.Type != MediaType.Picture) { return(BadRequest()); } var currentAvatar = await _ctx.Media.Include(d => d.User) .Where(d => (d.User.Id == user.AvatarId) && (d.Type == MediaType.Avatar)) .SingleOrDefaultAsync(); if (currentAvatar != null) { currentAvatar.Type = MediaType.Picture; _ctx.Update(currentAvatar); } media.User = user; media.Type = MediaType.Avatar; _ctx.Media.Update(media); await _ctx.SaveChangesAsync(); return(Ok(new { mediaId = media.Id })); }
public async Task <ActionResult> SetPersonAvatar(Guid id, Guid mediaId) { //todo change this after media upload is implemented var person = await _ctx.Persons.Where(p => p.Id == id).Include(d => d.Details).SingleOrDefaultAsync(); var mediaToAdd = await _ctx.Media.FindAsync(mediaId); if (person == null || mediaToAdd == null) { return(NotFound()); } if (!ModelState.IsValid) { return(BadRequest()); } if (mediaToAdd.Type != MediaType.Picture) { return(BadRequest()); } var currentAvatar = await _ctx.Media.Include(d => d.Details) .Where(d => (d.Details.Id == person.DetailsId) && (d.Type == MediaType.Avatar)) .SingleOrDefaultAsync(); if (currentAvatar != null) { currentAvatar.Type = MediaType.Picture; _ctx.Update(currentAvatar); } mediaToAdd.Details = person.Details; mediaToAdd.Type = MediaType.Avatar; _ctx.Update(mediaToAdd); await _ctx.SaveChangesAsync(); return(Ok()); }