// POST: api/Board public void Post([FromBody] SogoViewModels.Sogo sogo) { if (sogo.Name == null && sogo.Image == null && sogo.Audio == null) { return; } _repository.CreateSogo(sogo.Name, sogo.Image, sogo.Audio); }
// PUT: api/Board/5 public void Put(int id, [FromBody] SogoViewModels.Sogo sogo) { if (sogo.Name == null && sogo.Image == null && sogo.Audio == null) { return; } int audioId = id; _repository.UpdateSogo(sogo.Name, sogo.Image, sogo.Audio, audioId); }
public void DeleteSogo(int audioId) { ApplicationDbContext db = new ApplicationDbContext(); SogoViewModels.Sogo sogo = new SogoViewModels.Sogo(); sogo = db.Images.SqlQuery(@"SELECT * FROM dbo.Images INNER JOIN dbo.Audios ON dbo.Images.Id = dbo.Audios.ImageId WHERE dbo.Audios.Id = @p0", audioId).Select(d => new SogoViewModels.Sogo { Name = d.Name, //ProfileId, Name, FileName Id = d.ProfileId }).First(); string oldAudioFileName = "FriendBoard/FriendBoard/Audio/" + sogo.Id + sogo.Name + ".wav"; string oldImageFileName = "FriendBoard/FriendBoard/Images/" + sogo.Id + sogo.Name + ".png"; db.Database.ExecuteSqlCommand(@"DELETE FROM dbo.Audios WHERE Id = @p0", audioId); db.SaveChanges(); db.Database.ExecuteSqlCommand(@"DELETE FROM dbo.Images WHERE Id = @p0", audioId); //this would be better if you used imageId instead db.SaveChanges(); //It worked, it actually deleted it File.Delete(oldAudioFileName); File.Delete(oldImageFileName); }
public void UpdateSogo(string sogoName, string imageBytes, string audioBytes, int audioId) { ApplicationDbContext db = new ApplicationDbContext(); //File io projects need to be C:\Program Files (x86)\IIS Express\ /* string startupPath = System.IO.Directory.GetCurrentDirectory(); * string startupPath2 = Environment.CurrentDirectory; * int hmm = startupPath.Length; * string more = Directory.GetParent(Directory.GetCurrentDirectory()).Parent.FullName; * string testPath = System.IO.Path.GetFullPath(@"..\..\"); * string winDir = System.Environment.GetEnvironmentVariable("FriendBoard"); * string[] dirs = Directory.GetDirectories(winDir); * * //change then name of both existing files then update database * string[] files = Directory.GetFiles(@"\\Audio\", "*.wav", SearchOption.AllDirectories); * var filelist = new DirectoryInfo(@"\\Audio\").GetFiles("*.wav", SearchOption.AllDirectories); */ SogoViewModels.Sogo sogo = new SogoViewModels.Sogo(); //get profileID and Name so you know the name of the current file //db types //how do i find existing file name a: by going to the database first sogo = db.Images.SqlQuery(@"SELECT * FROM dbo.Images INNER JOIN dbo.Audios ON dbo.Images.Id = dbo.Audios.ImageId WHERE dbo.Audios.Id = @p0", audioId).Select(d => new SogoViewModels.Sogo { Name = d.Name, //ProfileId, Name, FileName Id = d.ProfileId }).First(); string oldAudioFileName = "FriendBoard/FriendBoard/Audio/" + sogo.Id + sogo.Name + ".wav"; string oldImageFileName = "FriendBoard/FriendBoard/Images/" + sogo.Id + sogo.Name + ".jpg"; //function(); //my javascript front will never send a sogo with all three parameters so it's best to just set up if-statements if (sogoName != null) { string newImageFileName = sogoName + ".png"; string newAudioFileName = sogoName + ".wav"; db.Database.ExecuteSqlCommand(@"UPDATE Images SET FileName = @p0, Name = @p1 WHERE Id = @p2", newImageFileName, sogoName, audioId); db.Database.ExecuteSqlCommand(@"UPDATE Audios SET FileName = @p0 WHERE Id = @p1", newAudioFileName, audioId); db.SaveChanges(); newImageFileName = sogo.Id + newImageFileName; newAudioFileName = sogo.Id + newAudioFileName; File.Move(oldAudioFileName, newAudioFileName); File.Move(oldImageFileName, newImageFileName); } else if (imageBytes != null) //Don't need to talk to database { //remove existing file and create new or update existing (simply replace byte content) //We need more customization, right now, I'll override existing names with null by accident string filePath = oldImageFileName; imageBytes = imageBytes.Replace(' ', '+'); File.WriteAllBytes(filePath, Convert.FromBase64String(imageBytes)); } else //Don't need to talk to database { File.Delete(oldAudioFileName); string filePath = oldAudioFileName; audioBytes = audioBytes.Replace(' ', '+'); File.WriteAllBytes(filePath, Convert.FromBase64String(audioBytes)); } }