public long AddFileToDb(byte[] fileContent, string ext, string userId) { Domain.Models.File file = new Domain.Models.File() { FileContent = fileContent, FileExtension = ext, UserId = userId }; appDb.Files.Add(file); appDb.SaveChanges(); return(file.Id); }
public async Task <AccountResponse> ChangeProfileImageAsync(User user, ProfileImageChangeViewModel profileImageChangeViewModel) { if (user != null) { if (profileImageChangeViewModel.profileImage != null) { var fileExtension = Path.GetExtension(profileImageChangeViewModel.profileImage.FileName); var fileContent = convertFormFileToByteArray(profileImageChangeViewModel.profileImage); Domain.Models.File profileImage = new Domain.Models.File { FileExtension = fileExtension, FileContent = fileContent, User = user, UserId = user.Id }; // remove previous profile image var previousProfileImage = await appDb.Files.Where(x => x.UserId == user.Id && x.Id == user.FileAvatarId).FirstOrDefaultAsync(); if (previousProfileImage != null) { appDb.Files.Remove(previousProfileImage); await appDb.SaveChangesAsync(); } // add new profile image await appDb.Files.AddAsync(profileImage); await appDb.SaveChangesAsync(); user.FileAvatarId = profileImage.Id; await appDb.SaveChangesAsync(); return(new AccountResponse { ResponseStatus = Status.Success, Message = "Succesfully changed profile image" }); } } return(new AccountResponse { ResponseStatus = Status.Error, Message = "User not found" }); }