示例#1
0
        public async Task <UserViewModel> SetUserImage(IFormFile file, string userID)
        {
            User user = await _users.FindByIdAsync(userID);

            if (user.PhotoID != 1)
            {
                _files.Files.Remove(_files.Files.FirstOrDefault(f => f.Id == user.PhotoID));
                _files.SaveChanges();
            }
            var model = _files.SaveFile(file);

            user.PhotoID = model.Id;
            await _users.UpdateAsync(user);

            return(new UserViewModel
            {
                Id = user.Id,
                Photo = new ImageData
                {
                    ImageBinary = model.Data,
                    ImageHeaders = model.ContentType
                },
                UserName = user.Name
            });
        }
        public async Task <ChatViewModel> SetChatImage(IFormFile file, int chatID)
        {
            var  model = _files.SaveFile(file);
            Chat chat  = _application.Chats.FirstOrDefault(chat => chat.Id == chatID);

            if (chat == null)
            {
                return(null);
            }
            chat.PhotoID = model.Id;
            _application.Update(chat);
            await _application.SaveChangesAsync();

            return(_chatConverter.ToViewModel(chat));
        }