public Models.Pet SetPet(Models.Pet pet) { using (DbConnection database = new SqlConnection(databaseConnectionString)) { var petDao = new PetDAO() { Guid = Guid.NewGuid(), Name = pet.Name, CategoryGuid = pet.Category.Guid, PetStatus = pet.Status, }; if (CheckIfCategoryExist(pet.Category.Guid, pet) == null) { return(null); } const string insertQuery = "INSERT INTO PetStore.Pet VALUES (@guid, @name, @categoryGuid, @petStatus)"; database.Execute(insertQuery, petDao); var newPet = TransformDaoToBusinessLogicPet(petDao); var tags = CreateTags(pet.Tags, petDao.Guid); if (tags == null) { return(null); } newPet.Tags = tags; return(newPet); } }
private Category CheckIfCategoryExist(Guid categoryGuid, Models.Pet pet) { using IDbConnection database = new SqlConnection(databaseConnectionString); var categories = database.Query <Category>("SELECT * FROM PetStore.Category").ToList(); var category = GetCategoryByGuid(categoryGuid); var categoryList = categories.Where(c => c.Name == pet.Category.Name && c.Guid == pet.Category.Guid); return(!categoryList.Any() ? null : category); }
public Models.Pet UpdatePet(Guid guid, Models.Pet pet) { using IDbConnection database = new SqlConnection(databaseConnectionString); var tags = CreateTags(pet.Tags, guid); if (CheckIfCategoryExist(pet.Category.Guid, pet) == null || tags == null) { return(null); } const string sql = "UPDATE PetStore.Pet SET name = @name, categoryGuid = @categoryGuid, petStatus = @status WHERE guid = @petGuid"; database.Execute(sql, new { name = pet.Name, categoryGuid = pet.Category.Guid, status = pet.Status, petGuid = guid }); return(pet); }